Jquery Event.target does not work on IE8

Asked

Viewed 96 times

2

Hi. I have a problem with IE8. The code below works on Chrome and firefox, but not on IE. As soon as it reaches that first line, the IE error says "expected object".

What I have is an img inside a div#Prods. When clicked, this code tests [if what was clicked is an img "&&" if its id is different from "#img-display" (id of another img inside the div that should not trigger the function) "&&" if the id is different from the value of eh_tb].

$('#prods').on('click', find("img"), function(event) {
    if( $(event.target).is('img') && ($(event.target).attr('id') != 'img-display') &&
($(event.target).attr('id') != eh_tb) ) {

        //PROCESSAMENTO AQUI
    }                                              
});

What’s in HTML is a menu. And an empty div#Prods. When I click on a menu link, it calls a PHP function by ajax to display the contents of a folder within #Prods.

The PHP code places inside #Prods:

  • a series of imgs that are thumbnails (these are the imgs tested in the function qd clicked);
  • a "p";
  • an "img" which is the #img-display;

OBS.: the variable "eh_tb" stores which thumbnail is currently displaying its content. It is just not to load the function and end up loading the same content again.

I had to use Event.target because the content is dynamically generated by PHP. Someone has a solution for this code to run on IE8?

  • 1

    What is this find("img")? And you can use this, but event.target should work, jQuery normalizes it.

  • Test this code: http://jsfiddle.net/Sergio_fiddle/a4rLmb8m/ - the reason to use a library like jQuery is to be compatible on different browsers. Unless you are using version 2.0 IE should work well. But as bfavaretto said, find(img) is wrong there

  • Related: http://answall.com/questions/63395/quando-usar-this-e-quando-usar-event-target

  • Related: http://answall.com/questions/51206/qual-a-diferen%C3%A7a-do-this-e-do-Event-target/51207#51207

1 answer

2


jQuery (in versions 1.x) works in the same way on IE8 and other more modern browsers. This is a great advantage of using a library.

That said, your problem is in the code.
Remove find("img") and place only "img" like @bfavaretto indicated. So delegate the event and that alone will cause you to remove your first condition in your if.

Inside the parameter/callback function .on() can use the this instead of event.target and you don’t need jQuery to get the ID.

My suggested code is:

$('#prods').on('click', "img", function(event) {
    if (this.id == 'img-display' || this.id == eh_tb) return false;
    //PROCESSAMENTO AQUI

});
  • It was perfect, I just add that in this situation eh_tb this refers to the value of a variable and not to an element with id="eh_tb", I don’t know if that’s the real intention of @Dtag. jsfiddle with the solution I made while you answered...

  • @Jader Dtag wrote in the question "&& id is different from the value of eh_tb", hence he did so.

  • yes, but eh_tb is a variable, and "eh_tb" is a string.

  • @Jader, therefore, we will have to wait for clarification.

  • Thank you all very much, especially @Sergio. It worked perfectly using $(this). attr('id'). Find('img') came from a solution I had researched. I don’t remember now, but I had understood (obviously wrong) that only "img" would not work. So simple, but I’m learning. hahaha. Thanks again! Abçs

  • @Jader eh_tb is actually the variable. The Thumbs have id=tb[a number]. So, for example, eh_tb = 'tb1', means the first Thumb is active. Ah, now I understand your confusion. I put in quotes just to highlight the text. I have this mania, but I saw now q can be confusing. It was bad.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.