Home jquery Get object id using jQuery

Get object id using jQuery

Author

Date

Category

there is a piece of html

& lt; div class = 'row' & gt;
   & lt; div class = 'col-md-1' style = 'width: 60px' & gt; Year: & lt; / div & gt;
   & lt; div class = 'col-md-5' style = 'margin-top: -25px' & gt;
    & lt; ul id = 'year' class = 'pagination' & gt;
      & lt; li class = 'active' id = '2016' onclick = 'year (this.id);' & gt; & lt; a href = '#' & gt; 2016 & lt; / a & gt; & lt; / li & gt;
    & lt; / ul & gt;
   & lt; / div & gt;
  & lt; / div & gt;
  & lt; div class = 'row' & gt;
   & lt; div class = 'col-md-1' style = 'width: 60px' & gt; Week: & lt; / div & gt;
   & lt; div class = 'col-md-5' style = 'margin-top: -17px' & gt;
    & lt; ul id = 'week' class = 'pagination' & gt;
      & lt; li id ​​= '2' onclick = 'week (this.id);' & gt; & lt; a href = '#' & gt; 2 & lt; / a & gt; & lt; / li & gt;
      & lt; li id ​​= '3' class = 'active' onclick = 'week (this.id);' & gt; & lt; a href = '#' & gt; 3 & lt; / a & gt; & lt; / li & gt;
    & lt; / ul & gt;
   & lt; / div & gt;
  & lt; / div & gt;

I’m trying to get the value of the active week

$ ("# week ul li.active"). attr ('id');

I get 2016, i.e. shows the year, not the week.
I can’t understand what the jamb is.


Answer 1, authority 100%

In html error: 3 & lt; / a & gt; / li & gt; , but it should be 3 & lt; / a & gt; & lt; / li & gt;

To get 3, write this:

var v = $ ("# week .active"). text ();

or so

var v = $ ("# week .active"). attr ("id"):

if there are several tags with id = “week” on the page, and you want to get the last one, then like this

var v = $ ("# week .active"). last (). attr ("id");

UPDATE

Because there is no li tag in the selector "#week .active" , then the selector will work even if the html is changed to the following:

& lt; div class = 'col-md-5' style = 'margin-top: -17px' id = 'week' & gt;
 & lt; div id = '5' class = 'active' onclick = 'week (this.id);' & gt; & lt; a href = '#' & gt; 5 & lt; / a & gt;
& lt; / div & gt;

Answer 2, authority 90%

This is all due to the wrong selector

$ ("# week ul li.active")

this selector looks for a list ul inside an element with id = "week" . And your own ul has this id .

Thus, we need to change this selector, saying that we are looking for exactly ul with the same id

$ ("ul # week li.active")

or omit ul

altogether

$ ("# week li.active")

since id must be unique on the page this selector is enough


As for

I get 2016, i.e. shows the year, not the week.

judging by the provided piece of code – the output of your selector should be undefined .
If you get exactly 2016 , we can assume that this piece of code is located inside a container with id = "week" , i.e. on the page you have at least two elements with this id.

Example

console.log ($ ("# week ul li.active"). attr (' id '));
$ ('body'). append ($ ("# week ul li.active"). attr ('id'));
console.log ($ ("# week ul li.active")); 
& lt; script src = "https://ajax.googleapis.com/ajax/libs /jquery/2.1.1/jquery.min.js"></script>
& lt; div id = "week" & gt;
 & lt; div class = 'row' & gt;
  & lt; div class = 'col-md-1' style = 'width: 60px' & gt; Year: & lt; / div & gt;
  & lt; div class = 'col-md-5' style = 'margin-top: -25px' & gt;
   & lt; ul id = 'year' class = 'pagination' & gt;
    & lt; li class = 'active' id = '2016' onclick = 'year (this.id);' & gt; & lt; a href = '#' & gt; 2016 & lt; / a & gt; & lt; / li & gt;
   & lt; / ul & gt;
  & lt; / div & gt;
 & lt; / div & gt;
 & lt; div class = 'row' & gt;
  & lt; div class = 'col-md-1' style = 'width: 60px' & gt; Week: & lt; / div & gt;
  & lt; div class = 'col-md-5' style = 'margin-top: -17px' & gt;
   & lt; ul id = 'week' class = 'pagination' & gt;
    & lt; li id ​​= '2' onclick = 'week (this.id);' & gt; & lt; a href = '#' & gt; 2 & lt; / a & gt; & lt; / li & gt;
    & lt; li id ​​= '3' class = 'active' onclick = 'week (this.id);' & gt; & lt; a href = '#' & gt; 3 & lt; / a & gt; & lt; / li & gt;
   & lt; / ul & gt;
  & lt; / div & gt;
 & lt; / div & gt;
& lt; / div & gt; 

Answer 3, authority 80%

You have an error here:

$ ("# week ul li.active"). attr ('id');

ID week this is the ul list, it should be like this:

$ ("# week li.active"). attr ('id');

Answer 4

Eh .. Nobody remembers the prop

$ ("# week .active"). prop ('id');

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions