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?
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.
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.
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.
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");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With