0

I am trying to compare a custom attribute of a given element to that custom attribute of all other elements with a specific class... here is what I have

function choose(el){
    var text = $(el).getAttribute("data-custom");
    var list = document.getElementsByClassName("class");
    for (var i = 0; i < list.length ; i++) {
        if (list[i].getAttribute("data-custom") == text) {
            /*DO STUFF*/
        }
    }
}

html

<div onclick="choose(this)">STUFF</div>

Currently I get a "$(...).getAttribute is not a function" error.

1
  • 2
    the function is attr() Commented Jun 30, 2015 at 17:14

2 Answers 2

1

The jQuery version is $().attr('data-custom'). Note that to access data-* attributes, you can use $().data('custom').

Or you could ditch jQuery (you didn't use it in the very next line) and use

el.getAttribute('data-custom');
// If el is not a DOM element
$(el)[0].getAttribute('data-custom');

Even better, if you don't have to support IE < 11, you can use dataset

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

Comments

0

You are mixing javascript and jquery.

$(...).attr('your_attr')

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.