HI WELCOME TO KANSIRIS

jQuery Get All Images (img) Src in Div with Example

Leave a Comment
In jQuery to get images (img) src tags in div element we need to write the code like as shown below


$('#divimages').children('img').map(function () {
return $(this).attr('src')
}).get()

If you want complete example to get image tags in particular div we need to write the code like as shown below


<html>
<head>
<title> jQuery Get All the Images in One Div </title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$('#btnGet').click(function () {
var imgs = $('#divimages').children('img').map(function () {
return $(this).attr('src')
}).get()
alert(imgs);
})
})
</script>
</head>
<body>
<b> Get All the Images in Div </b>
<div id="divimages">
<img src="test.png" />
<img src="test2.jpg" />
</div>
<input type="button" id="btnGet" value ="Get Images" />
</body>
</html>

Demo

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.