1

I'm having trouble with some Javascript, and hopefully someone may be able to help! First, here is the code:

function Client_Selected() {
    var temp = $("input:radio[name='selected_client']:checked").val();
    var f_input_name = "fname_" + temp;
    var l_input_name = "lname_" + temp;    
    var first = $("input[name='"+f_input_name+"']").val();
    var last = $("input[name='"+l_input_name+"']").val();
    var content = first + " " + last;
    var thispage = $("input[name='current_page']").val();

    // Now Load the Notes!!!
    dataString = 'web_id='+temp+'&Section='+thispage;
    $.ajax({
        type: "POST",
        url: "<?=URL_SSL_WEBSITE_ROOT?>/notes/loadClientNotesAJAX",
        data: dataString,
        success: function(msg) {
            $("#notes_body").html(msg);
            $("#note_client_name").html(content);
        }
    });        

    $("#button_client_name").html(content);
    $("#note_client_name").html(content);    
    $("input:radio[name=selected_client]:checked").attr(checked, true);
};

My problem is with the last line. What's going on, is I have a list of clients who are associated with a radio button, when a button is clicked I make an AJAX call to get/display information about the client. If the last line is not present, then when I select a second client, it loads the information but the new client's radio button does not stay checked and instead the previous (or first) selected clients radio button remains checked.

With the last line I get an error of: "checked is undefined"

despite the error everything worked and the appropriate checkbox remained checked. However, with the new update to firefox yesterday ... the appropriate checkbox remains checked, though my success function never runs on the ajax call, and the javascript simply stops on the last line. It still works in IE, but I need to get this working again in firefox. Any Ideas?

I've tried changing it to: $("input:radio[name=selected_client]:checked").attr('checked', true); Which then the success function runs, and the information is populated on my webpage, however I then get the problem of the newly selected checkbox NOT remaining checked and the previous one remains checked.

Any Help is much appreciated.

Thanks

2
  • Can you please show the code as the browser sees it, after being parsed by PHP?
    – Anonymous
    Commented Jan 6, 2010 at 14:35
  • the url it's getting is (after being parsed by php) is correct. Firebug is showing the correct response from the ajax call, however, it's freezing when it hits the last line. I tried chaning it to ('checked','checked') ... that does not error and the success function runs, but it still reverts to the previous radio button being checked with that change. I can try telling it which radio button to check by the radio button's value. Can someone give me the code to try that, I've been unable to find it, as I'm sure it's not needed that often!!! Thanks for the answers thus far!
    – Brian
    Commented Jan 6, 2010 at 14:46

4 Answers 4

1

.attr('checked','checked'); will check your checbox/radio box and .removeAttr('checked'); to uncheck it.

1

Looks like some quotes are missing

$("input:radio[name=selected_client]:checked").attr('checked', 'checked');
1
  • well, this doesn't give me the error. But it's also not keeping the newly selected radio button checked. It is still reverting back to the previous one ... any ideas? Thanks
    – Brian
    Commented Jan 6, 2010 at 14:54
1

try the last line in the success function of the ajax call.

and use attr('checked',true)

1
  • 1
    $(...).attr( 'checked', true )
    – DmitryK
    Commented Jan 6, 2010 at 14:42
1

checked is undefined

It certainly is. You used checked; JavaScript is looking for a variable called checked, which doesn't exist. You meant to say 'checked' with quotes, as a string.

However,

$("input:radio[name=selected_client]:checked").attr('checked', true);

Does nothing because you are checking a radio which by definition from your selector is already checked. What are you trying to do, here?

Note that if you are doing this in a radio button onclick handler, the :checked radio during the execution of the function will not be the newly-clicked one, it'll be the one that was previously checked. It is not until onclick event handling is finished that the click-to-change-checkedness actually goes through. If you want to find out the new radio during an onclick, you will have to look at this or the event object, depending on how you're calling the function.


Re other answers:

attr('checked','checked')

Can we please stop giving this out? It works, but not because of the reasons you think. jQuery attr() does not set HTML attributes (except in a couple of corner cases trying to work around browser bugs). It sets JavaScript properties, but trying to fix up the names to match the HTML attributes. So:

attr('checked', true);

or false for unsetting, is the correct approach. The value 'checked' only works because it is truthy when evaluated for a boolean property. Any non-empty string would do exactly the same.

If jQuery did set HTML attributes, attr('checked') would not work, because the HTML checked attribute corresponds to the initial setting of the checkbox (aka defaultChecked), not the current checkedness state.

Not the answer you're looking for? Browse other questions tagged or ask your own question.