1

I have a <ul> with many <li>. My <li>'s have different id's: for example: <li class="image" id=1>....</li> How can I find now this <li> with the id=1 ?? I tried this-->

var deleteImage =  $("ul").find( ".image" ).attr("id").val(1);

...but it doesnt work. Any idea? THANKS!!!

Zwen2012
  • 3,360
  • 9
  • 40
  • 67
  • 4
    What about `$('#someId')`? Since the ID values should be unique throughout the document. – James Allardice Nov 05 '14 at 15:47
  • http://api.jquery.com/id-selector/ – Tobías Nov 05 '14 at 15:47
  • HTML4 and below require non numeric IDs btw – Alex K. Nov 05 '14 at 15:49
  • If you use specific ids it's unnecessary to look search your ul's children, since ids are considered to be unique in the whole document. If that isn't the case, you might run into some weird behaviour with some browsers. So ids should always be used uniquely and the mehtod suggested by James Allardice works – Markai Nov 05 '14 at 15:50
  • You should learn how to use jQuery [selectors](http://api.jquery.com/category/selectors/). Its very easy to learn if you study it. – Leysam Rosario Nov 05 '14 at 15:54
  • @Markai, that's not entirely true. If you wanted to address an id if it has a certain context, then you wouldn't use it alone. Imagine you have a `toto` id that can be put on variable elements (but only once per page), and that you want to style it ONLY if it's inside the `.foo` div then you'd have to use the selector `.foo #toto`. – Antoine Combes Nov 05 '14 at 16:04
  • @Antoine Combes that's true, but nevertheless should ids be unique. – Markai Nov 05 '14 at 16:09

3 Answers3

6

This is a basic JQuery/CSS selector:

var deleteImage =  $("ul").find( "#1.image" );
Antoine Combes
  • 1,444
  • 8
  • 13
5

ID's should never start with numbers. Change your id's to image1, image2, image3, ... Then use:

$('#image1').functionToExecute();
Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
0

The id tag is supposed to be unique within the HTML document. So, assuming you've constructed the HTML appropriately, you should be able to select the li tag with just:

$('#1')