tags:

views:

1162

answers:

4

I have an anchor tag on my page that is added dynamically using jQuery:

<a href="#" id="ClickMe">Click Me</a>

I have the following jQuery within my $(document).ready(function()

 $("#ClickMe").live('click', function()
 {
     $("#SomeDiv1").show();
     $("#SomeDiv2").hide(); 
 });

This works fine in firefox, but not in IE. I am developing in IE8 currently, but I will need it to run in IE7 minimum.

+1  A: 

Why not use the click event directly instead of live ? You are binding to an element with an ID, so I presume that you won't have other elements added later on with the same ID; and that defeats the purpose of using the live function.


Try it like this :

$("#ClickMe").click(function()
 {
     $("#SomeDiv1").show();
     $("#SomeDiv2").hide(); 
 });
Andreas Grech
I should have stated the anchoir tag is added dynamically to the page.
littlechris
+1  A: 

The code probably is working just as it should, only it is not doing what you expect; as you are not doing anything from stopping the click event from causing browser navigation, that might disturb your showing and hiding of divs.

Try the following:

$('#ClickMe').live('click', function(ev) {
    $('#SomeDiv1').show();
    $('#SomeDiv2').hide();

    // Stop event handling in non-IE browsers:
    ev.preventDefault();
    ev.stopPropagation();
    // Stop event handling in IE
    return false;
});
Tomas Lycken
I have changed this. But I put an alert in to see if the event is even firing, and no alert pops up. It seems the live is not attachiong the event to the anchor in IE
littlechris
When you add the link, do you do so by manually constructing it in jQuery or with the result of an AJAX call?
Tomas Lycken
It is contained within a div that I then show using Google Maps Gmarker.openInfoWindowHtml(MyDiv). This is run in the $.Ready function.
littlechris
I can't find a method with that signature in the API reference - is your html added OK when you run it? Could you perhaps post a live example that we could test and experiment with?
Tomas Lycken
Posted in original question :)
littlechris
A: 

I have solved the issue by embeeding a call to a function in the anchor tag being rendered in the info window.

e.g.

> <a href="#" id="ClickMe" onclick="ClickMeFunc();">Click Me</a>
littlechris
This really isn't a good answer, because you just hacked your way around the problem instead of actually solving it.
Nick Berardi
Thanks for constructive critisim. I'll implement as you suggest... oh wait you didn't suggest anything... Thanks for the comment - not really that useful three months after I had the issue.
littlechris
I had the same problem as you littlechris. Anchor tags in Google Maps infowindows would not accept jQuery live events in IE. Worked fine in FF, Chrome etc. Your workaround is great - cheers! And Nick - hacking around the problem *is* solving it sometimes. Having all the JS handlers outside the HTML is nice, but this is Google Maps we're using - it's nothing but JS!
meanstreakuk
A: 

Hi littlechris & meanstreakuk would you give some more detail on code? i'm facing the same problem.. i use some trigger like u suggested above, but it still wont work. Should i keep the live function? or i should remove it?

thank you

Ronggur
Hi ronggur, I've not been on stack for a few days... do you still need help? i'll dig out my solution if you do.
littlechris