and this is js $...">

Jquery functions each and attr

I have this html:

<input id="testme" test="something"/><label test="something2"></label>

and this is js

$("[test]").each(alert($(this).attr("test")));

given here:

jsfidde

I would have thought that a warning would give me "something", and then "something2". But he does nothing!

What's happening?

+3
source share
2 answers

You warn the wrong thing. eachjust returns the / jQuery collection. You will need to warn inside each callback to alert the values ​​of the custom attribute. Also. Please use the prefix data-when assigning [custom attributes] [1] to better meet standards.

$(".classname").each(function(){
    alert($(this).attr("classname"));
});
+13
source

.each() accepts a function, it should look like this:

$("[test]").each(function() {
  alert($(this).attr("test"));
});

Here you can check it out .

+1
source

Source: https://habr.com/ru/post/1783662/


All Articles