1

Situation:

I have the following jquery:

addButton();

function addButton(){
    var div = $("#mydiv");
    div.append(addHtml());
    addEvents(div);
}

function addEvents(div){
div.find(".toggleButton").button();
div.find(".toggleButton").click(function(e) {
    alert("test");
});
}

function addHtml(div){
return "<input class=\"toggleButton\" type=\"checkbox\" id=\"id1\" name=\"name1\" /><label for=\"id1\">Yes</label>";
}

The checkbox turns into a button, but does not signal the alert on click. Why does the button part work but not the click part???

Update: This works in firefox but not in ie-8. Unfortunately, I need it work in ie-8.

Update 2: If I comment out the .button call the click part works in ie-8. How do i get both to work together?

Update 3: Actual html generated in firefox:

<input class="toggleButton ui-helper-hidden-accessible" id="id1" name="name1" type="checkbox"><label aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" aria-pressed="false" for="id1"><span class="ui-button-text">Yes</span></label>

Actual html generated in IE-8:

<INPUT id=id1 class="toggleButton ui-helper-hidden-accessible" type=checkbox name=name1 jQuery1294922695438="218"><LABEL aria-disabled=false class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role=button aria-pressed=false for=id1 jQuery1294922695438="223"><SPAN class=ui-button-text jQuery1294922695438="225">Yes</SPAN></LABEL>

6
  • haha we both made the edit at the same time - thanks :) Commented Jan 13, 2011 at 12:09
  • Just to make sure, the HTML is escaped because it's for an echo or something (i.e. the browser doesn't see the \") Commented Jan 13, 2011 at 12:10
  • @john - sorry, it's escaped because i'm adding ti via javascript. It's actually a string in javascript that i'm appending. Commented Jan 13, 2011 at 12:12
  • @john - div is a jquery object. It's some continer, generally a div, that the input was just added to. Then the events are added. Maybe I should post a little more code... Commented Jan 13, 2011 at 12:13
  • @everyone - updated with a little more code to give a better picture of what i'm doing. Commented Jan 13, 2011 at 12:19

3 Answers 3

4

The issue is with IE-8's decision on what activates the click. In IE-8 the label is what drives the click, so you must attach the click event to the label!

Also, you cannot have the same class on both the input and the label; for it to work properly in both ie-8 and firefox you must do this:

function addEvents(div){
div.find(".toggleButtonInput").button();
div.find(".toggleButtonLabel").click(function(e) {
    alert("test");
});
}

function addHtml(div){
return "<input class=\"toggleButtonInput\" type=\"checkbox\" id=\"id1\" name=\"name1\" /><label class=\"toggleButtonLabel\" for=\"id1\">Yes</label>";
}

And then it works properly.

Sign up to request clarification or add additional context in comments.

1 Comment

Of course this makes it rather a pain to get to the checked attribute inside of the click function.
1

You can use an id selector which will be the fastest and use .live() to bind the click event since you are generating element at the runtime.

$("#id1").live("click", function(){
    alert("clicked");
});

4 Comments

I tried to use delegate, but that did not work either. So I went to this to sorta debug the problem. However this does not work either. Clearly the above code should work... If the button part works the click part should work...?
Try inspecting in firebug what is the exact HTML generated after applying .button().
<input class="toggleButton ui-helper-hidden-accessible" id="id1" name="name1" type="checkbox"><label aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" aria-pressed="false" for="id1"><span class="ui-button-text">Yes</span></label> in firefox
<INPUT id=id1 class="toggleButton ui-helper-hidden-accessible" type=checkbox name=name1 jQuery1294922695438="218"><LABEL aria-disabled=false class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role=button aria-pressed=false for=id1 jQuery1294922695438="223"><SPAN class=ui-button-text jQuery1294922695438="225">Yes</SPAN></LABEL> in ie=8
0

I would use:

$(".toggleButton", div).click(function(e) {
    alert("test");
});

12 Comments

No messages whatsoever in ie developer tools
Just for test, try run it in Firefox
Here is your porblem... var div = $("mydiv"); It should be var div = $("#mydiv");
I hate my life... lol it works in firefox. However it must work in ie. In the end it working in firefox is irrelevant because the clients will only use ie-8
@vladimir - that was a typo into the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.