Reputation: 83
I have this code in my page:
$('img[title!=""]').each(function() {
It currently selects the all img
's in the website, but I only want to get the images that are within a certain div called 'layout' where title is not null, how would I do this?
Upvotes: 0
Views: 45
Reputation: 40038
First, you might need to allow for those elements that don't have a title attribute at all. This condition would be covered by:
$('#layout img[title]').each(function() {
However, then you must check if the title attribute is present but blank:
if ( this.title != "" )
Putting it together:
HTML:
<div id="layout">
<img title="kitten01" src="http://placekitten.com/150/150" />
<img src="http://placekitten.com/151/151" />
<img title="" src="http://placekitten.com/152/152" />
<img title="kitten04" src="http://placekitten.com/153/153" />
</div>
<div id="otherdiv">
<img title="dog01" src="http://placedog.com/150/150" />
<img title="dog02" src="http://placedog.com/150/150" />
<img title="dog03" src="http://placedog.com/150/150" />
</div>
jQuery/js:
$('#layout img[title]').each(function() {
if ( this.title != "" )
alert( this.title );
});
Note: you may already know this, but fwiw the above example assumes that your div is called layout
because it has an ID attribute named layout. If the div is of class="layout"
then:
$('.layout img[title]').each(function() {
Upvotes: 0
Reputation:
$("#myDiv").find('img[title!=""]').each(function() {
// ...
});
That would catch all <img>
in #myDiv
:
<div id="myDiv">
<img src=" ... />
<img src=" ... />
<img src=" ... />
</div>
Upvotes: 0
Reputation: 133403
Assuming layout
is ID of div
Use
$('img[title!=""]', '#layout').each(function() {
OR
$('#layout img[title!=""]').each(function() {
Upvotes: 1