Jerome Ansia
Jerome Ansia

Reputation: 6884

jQuery Why i cannot change the radio button value once set

Here i got a problem in my script,

Once we set one radio button we cannot change it anymore :

$("#divAddNewNote").click(function() {
    if ($("#divAddNewNote textarea").length == 0) {
        $("#divAddNewNote div").css("display", "none");
        box += "<div class=\"noteBottomBar\">";
        box += "    <div class=\"radioPrivatePublic\">";
        box += "        <input type=\"radio\" id=\"radio1\" name=\"groupPrivate\" value=\"private\" /><label for=\"radio1\">Private</label>";
        box += "        <input type=\"radio\" id=\"radio2\" name=\"groupPrivate\" value=\"public\" /><label for=\"radio2\">Public</label>";
        box += "    </div>";
        box += "    <button onclick=\"ajaxSaveNewStatus(10886);\">Post</button>";
        box += "</div>";
        $("#divAddNewNote").append(box);

        return false;
    } else {
        return false;
    }

});​

It's a problem in my javascript code but i cannot find it

Upvotes: 1

Views: 1305

Answers (3)

James Montagne
James Montagne

Reputation: 78650

The problem is that your radio buttons are within #divAddNewNote. Your click handler for #divAddNewNote returns false. returning false in a jquery event handler has 2 functions. It is equivalent to event.preventDefault and event.stopPropagation. The important one here is the preventDefault. This is causing your attempt to change the value of the radio button to be canceled ("prevented").

Upvotes: 3

ShankarSangoli
ShankarSangoli

Reputation: 69905

$("#radio1").checked = true is not the correct way to check the radio button programmatically.

Use this $("#radio1").prop('checked', true)

Upvotes: 1

BluesRockAddict
BluesRockAddict

Reputation: 15683

Try using the following:

$('input:radio[name="myradiobutton"]:checked').prop('checked',false);

Upvotes: 1

Related Questions