1

Is there a jQuery selector I can use to target the value of an HTML5 data attribute, given the data is stored in JSON format?

For example, if I have something like <button data-id="5">Some action</button>, I can use $("button[data-id='5']") to select that button.

However, my data is stored as <button data-properties='{"id":5,"name":"foo"}'>Some action</button>. Is there a selector I can use for that, or do I have to use .filter() for example?

2 Answers 2

2
$("button[data-properties*='\"id\":5']")

closest i can think about right now is this

*= translates to "attribute contains selector"

\ is to remove special meaning from " and consider them characters instead

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this one.

$("button[data-properties]").each(function(){
    if($(this).data("properties").id == 5){
        //your action here
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.