1

I need to select elements by data attribute name AND data attribute value.

Something like (but obviously, it doesn't work)

for(var i=0; i<x.length; i++){
    var y = $('.my-class').attr('data-id', i); //trying to select here
    y.html('input' + i);                                
}

Have no idea how to achieve this, please help! :)

1 Answer 1

1

you can use attribute selector

var y = $('.my-class[data-id="' + i + '"]');

since the selector .my-class is repeated, you can cache it

var els = $('.my-class');
for(var i=0; i<x.length; i++){
    var y = els.filter('[data-id="' + i + '"]'); //trying to select here
    y.html('input' + i);                                
}

Demo: Fiddle

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.