Reputation: 571
I have this block of code in which I am trying first get the contents within a div, then match it to a string. If it matches...do something. However, this code appears to be matching everything.
The logic is this: 1. For each of the four footer sections 2. If the title of the section matches "Stay Connected" 3. Add this image
Thanks in advance for your help!
// ADD ARROW TO FOOTER
$(".footer-nav__title").each(function () {
var results = $( ".footer-nav__title" ).html();
var match = ("Stay Connected");
if($('results:contains(match)')) {
$(this).append('<img src="https://cdn.shopify.com/s/files/1/0013/8467/7443/files/footer-arrow.png?8377676856849539925" alt="" class="footer-arrow" />');
}
});
Upvotes: 0
Views: 82
Reputation: 503
results and match are both string variables, but you're not concatenating them correctly in your code. Even if you did however, you would not get the desired effect.
This should give you the result you're looking for:
var results = $(this).html();
var match = ("Stay Connected");
if (results.indexOf(match) > -1) {
$(this).append('<img src="https://cdn.shopify.com/s/files/1/0013/8467/7443/files/footer-arrow.png?8377676856849539925" alt="" class="footer-arrow" />');
}
Upvotes: 5
Reputation: 24965
/* Criteria from the OP
1. For each of the four footer sections
2. If the title of the section matches "Stay Connected"
3. Add this image
*/
//#1
$(".footer-nav__title").each(function(){
//#2
//Don't look up the element again, especially a global selector that will get them
//all. Use the one you are looping over.
if (this.innerHTML.indexOf('Stay Connected') > -1) {
//#3
$(this).append('<img src="https://cdn.shopify.com/s/files/1/0013/8467/7443/files/footer-arrow.png?8377676856849539925" alt="" class="footer-arrow" />');
}
});
Upvotes: 3