Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the img src inside a <li><div> tag?

I'm trying to get the src of an image from a selected list item. The HTML looks like this:

<li id="player_7">
  <div class="nailthumb-container square-thumb">
    <img src="../photos/files/7/thumb/6c0f1676cdcb1a9a5215db3e12572450.jpeg" />
  </div>
</li>

I can currently get the player ID from the selected list element like so:

jQuery

$('#myDiv').find("li").click(function() {
    var user_id = $(this).attr('id').substr(7);
});

How can I get the img src from a selected list element?

like image 681
Paul Avatar asked Oct 19 '12 18:10

Paul


People also ask

Can you put IMG in Li?

Firstly, you need to create the List Item using a li list item tag. Within that List Item tag, you can place your image. You add an image within the Image tag img.

How do you put an image in a div?

1) Create a DIV tag with a unique ID; 2) Place the image into a background:url style element of a DIV tag; 3) Set the height and width properties of the DIV tag to that of the selected image.

Can we use div tag inside li tag?

Yes you can use a div inside a li tag and it will validate it. They are no different in this sense and be valid in HTML4, XHTML and HTML5 as well.


1 Answers

If user_id corresponds to number in player ID, then try:

var src = $("#player_" + user_id + " img").prop("src");

Otherwise, respectively to the clicked list item:

$("#myDiv").find("li").click(function() {
    var src = $("img", this).prop("src");
});
like image 151
VisioN Avatar answered Oct 13 '22 19:10

VisioN