Вопрос

I have section and article for displaying contents. My code is:

<section id = "examples">

 <article id="item_1">
    ...
 </article>

 <article id="item_2">
  ...
 </article>

 <article id="item_3">
    ...
 </article>

 <article id="item_4">
  ...
 </article>

 ...

</section>

I am trying to get the id's of the articles so that I can compare, but I am getting undefined. My code is:

var compare = "item_1"

$('article').each(function(){
 var pos = $('article').id;
 console.log(pos) //getting undefined

    if( pos == compare)
     // do something

});
Это было полезно?

Решение

Use this to get each article element's id: var pos = this.id;:

EXAMPLE HERE

var compare = "item_1";

$('article').each(function () {
    var pos = this.id;
    console.log(pos);

    if (pos == compare) {
        alert('match');
    }

});

Output:

item_1
item_2
item_3
item_4 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top