Theres’ this html code:
<div class="wcpa_form_outer" data-attrrelated="["wcpa-select-1658734650073"]">
for which i’m trying to append html to it. I have tried various approaches but none have worked.
jQuery('.wcpa_form_outer[data-attrrelated="["wcpa-select-1658734650073"]"]').append('some html here');
or
jQuery('.wcpa_form_outer[data-attrrelated="[wcpa-select-1658734650073]"]').append('some html here');
or
jQuery('.wcpa_form_outer').data('attrrelated').append('some html here');
any clues?
2
Answers
The
"
and/or[]
in the attribute value may be the problem Remove it, or try using a part (the most relevant part?) of the attribute value:Problem is that you’re using the HTML Entity
"
in your attribute. This is being translated to a literal quote. JQuery does not do Entity translation, so it’s literally looking for the string["wcpa-select-1658734650073"]
with ampersands and all, not["wcpa-select-1658734650073"]
which is the actual value in your attribute.You can work around this by using one of the following methods (after also translating the Entity into a quote in your code).
attr*=value
) (demonstrated by KooiInc’s answer) orattr=value
), shown below'["' + value + '"]'
)decodeEntities
function from this answer to translate your attribute value before attempting the lookup (untested, and it’s 10 years old)