Mathias Colpaert
Mathias Colpaert

Reputation: 680

jquery hiding image on page load depending on title

I want to hide an image on page load, depending on its title This does not seem to work.. when i put this in my code, i get an alert for all the images that have a title (which is correct):

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        alert(this.title);
 }

});

In my CSS file I disabled the display of all images at first:

.contact a.tooltip{
    display: none;
}

now i want to show all the images with non empty title, which doesnt work:

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        this.show();
        alert(this.title);
 }

});

Upvotes: 2

Views: 742

Answers (4)

Mathias Colpaert
Mathias Colpaert

Reputation: 680

article a.tooltip img is not hidden. .contact a.tooltip is hidden.

Change

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        this.show();
        alert(this.title);
 }

});

to

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        $(this).parent().show();
        alert(this.title);
 }

});

Upvotes: 0

Talha Akbar
Talha Akbar

Reputation: 10030

$("article a.tooltip img").each( function() {
var t = $(this).attr("title");
if(t!=// some title) {
$(this).parent("a.tooltip").show();
}
else {
$(this).parent("a.tooltip").remove();
}

Upvotes: 0

Michael Antonius
Michael Antonius

Reputation: 972

Try to use $(this) and .attr() like this:

$('article a.tooltip img').load(function(){
  if($(this).attr('title') != ""){
    $(this).show();
    alert($(this).attr('title'));
  }
});

hope it can help

Upvotes: 0

Dávid Szabó
Dávid Szabó

Reputation: 2247

article a.tooltip img is not hidden. .contact a.tooltip is hidden.

Change

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        this.show();
        alert(this.title);
 }

});

to

$('article a.tooltip img').load(function(){
    if(this.title != ""){
        this.parent().show();
        alert(this.title);
 }

});

Upvotes: 4

Related Questions