I'm attempting to change the data-target attribute of an a tag based on the content of a corresponding div. I've tested a few different avenues and believe this to be the cleanest. However, as this is modifying multiple entries, only the first entry's data-target is being updated and it's being modified to market, rather than to affordable. The .html and .css changes are working as expected. It's only the data-target element that is causing an issue. You can see other variations of what I've tried commented out below.
jQuery(document).ready(function ($) { //replaces all instances of $ with jquery for Wordpress
$(".unit-rate-mapped div").each(function () {
if ($(this).is(':contains("mk")')) { //Only Market Rate units codes will contain mk
$(this).html('Market Rate'); //Change the HTML value to Market Rate
$(this).css({ "background": "rgba(4,57,99,.1)", "color": "#043963" });
$('#unit-qual-link').attr('data-target', '.fusion-modal.form-market'); //modify the data-target attribute of the pre-Qualify link to link to the market form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-market";
} else { //Anything and everything else goes to orange
$(this).html('Affordable'); //Change the HTML value to Affordable
$(this).css({ "background": "rgba(229,104,53,.1)", "color": "#e56835" }); //Change the background color and type to orange
$('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable'); //modify the data-target attribute of the pre-Qualify link to link to the afforrdable form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-affordable"; //modify the URL of the Pre-Qualify link to link to the affordable form
}
});
//Tried this variation as well
// if ($(".unit-rate-mapped div:contains('Market Rate')")) {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-market');
// } else {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable');
// }
});
Here's an example of the HTML I'm looking to modify:
<!--Market Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">mk1234</div>
</div>
<div class="unit-qualifications">
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target=".fusion-modal.form-market" href="#">Pre-Qualify</a></div>
</div>
<!--Affordable Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">1b1b60</div>
</div>
<div class="unit-qualifications">
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target=".fusion-modal.form-market" href="#">Pre-Qualify</a>
</div>
</div>
Edited to include @freedomn-m's recommendations, but the data attributes are coming back blank:
jQuery(document).ready(function ($) { //replaces all instances of $ with jquery for Wordpress
$(".unit-rate-mapped div").each(function () {
if ($(this).is(':contains("mk")')) { //Only Market Rate units codes will contain mk
$(this).html('Market Rate'); //Change the HTML value to Market Rate
$(this).css({ "background": "rgba(4,57,99,.1)", "color": "#043963" });
$('#unit-qual-link').data('target', '.fusion-modal.form-market'); //modify the data-target attribute of the pre-Qualify link to link to the market form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-market";
} else { //Anything and everything else goes to orange
$(this).html('Affordable'); //Change the HTML value to Affordable
$(this).css({ "background": "rgba(229,104,53,.1)", "color": "#e56835" }); //Change the background color and type to orange
$('#unit-qual-link').data('target', '.fusion-modal.form-affordable'); //modify the data-target attribute of the pre-Qualify link to link to the afforrdable form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-affordable"; //modify the URL of the Pre-Qualify link to link to the affordable form
}
});
//Tried this variation as well
// if ($(".unit-rate-mapped div:contains('Market Rate')")) {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-market');
// } else {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable');
// }
});
Result with blank data-target:
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target="" href="#" data-uw-styling-context="true">Pre-Qualify</a>