views:

92

answers:

2

Hi guys,

Using Javascript and html:

I have a list containing radio buttons that is dynamically loaded at launch time based on stored data. Even though the stored value of the radio is "on" the radio at launch does not display as selected.

I cant figure out what I'm missing.

if (defsales !== undefined) {
    $('.defsales', newRow).val(defsales); }

defsales being the class identifier of the radio.

Do I need an additional argument to check the value? Or, do I need to add something that reapplies the value after launch?

I've been working at this problem long enough that I'm getting confused.

Please excuse my ignorance,

If it helps, here is the rest of the function:

function addSalespersonRow(id, name, phone, email, defsales) {
var newRow = $('<tr>'+
    '<td><input type="hidden" class="id" /><input class="name" size="20" /></td>'+
    '<td><input class="phone" size="15"/></td>'+
    '<td><input class="email" size="30"/></td>'+
    '<td><input type="radio" name="salespersons" class="defsales" /></td>'+
    '<td><a href="#" onclick="$(this).parent().parent().remove(); return false;">Delete</a></td>'+
    '</tr>');
if (id === undefined) {
    id = new Date().getTime();
}
$('.id', newRow).val(id);


if (name !== undefined) {
    $('.name', newRow).val(name);
}
if (phone !== undefined) {
    $('.phone', newRow).val(phone);
}
if (email !== undefined) {
    $('.email', newRow).val(email);
}
if (defsales !== undefined) {
    $('.defsales', newRow).val(defsales);
}
$('#salespersons-table tbody').append(newRow);

return false;
}

Here was the code that worked correctly.

if (defsales !== undefined && defsales == "on") {
    $('.defsales', newRow).attr('checked', true);
}

Thanks to Matt, for pointing me in the right direction.

A: 

I'm not entirely sure what it is you're doing outside of this problem or more importantly why you're doing it... but I think this may solve/help with your problem (untested).

if (defsales !== undefined) {
  $('.defsales').each(function() {
    $(this, newRow).val(defsales);
    if($(this).val() == 'on') {
      $(this).attr('checked', 'checked');
    }
  });
}
Steve
Thanks Steve,I tried using your suggestion, but it didn't work.
Eric Hunt
+1  A: 

Using JQuery you should be able to check any radio button like this.

$(".dfsales").attr("checked", true); 

EDIT:

This does seem to work:

$(document).ready(function(){
    var dfsales = 1;
    $('<input type="radio" name="colorInput" id="test" value="2">').appendTo($("#list"));
    if(dfsales !== undefined){
        $('#test').attr('checked', true);
    }
})

EDIT 2: Eric, assuming you only care to set the value of the radio button if dfsales == on this will work.

if (defsales !== undefined && defsales == 'on') {
    $('.defsales', newRow).val(defsales).attr('checked', true);
}

if you want the radio button value to be set regardless, then try something like this:

if (defsales !== undefined){
    $('.defsales', newRow).val(defsales);
    if(defsales == 'on'){
        $('.defsales', newRow).attr('checked', true);
    }
}

Does this help?

Matt
Thanks Mike, but your suggestion didn't work either.
Eric Hunt
Eric, and you're sure that dfsales is not set to undefined?Are the radio buttons being added in after the page has completely rendered? I tested the code (shown in my edit above) and it runs just fine. Are you using Ajax to pull the raido buttons in?
Matt
Thanks Matt, I'm new enough that i'm not sure what JQuery or Ajax is, but I'm pretty sure the radios are being loaded as the page is being populated (or compiled). this project is using Javascript and html.you can see the whole problem here: http://stackoverflow.com/questions/2290799/how-to-limit-checkbox-selections-to-one-in-a-html-document-when-the-list-containi
Eric Hunt
Sorry Matt, No luck! I really appreciate your efforts. Btw. your last example prevented the entire list from showing. I really thought your 3rd snippet was going to work. bummer,
Eric Hunt
are you getting any kind of errors?
Matt
I had a typo in that last snippet. I had dfsales instead of defsales, I updated it.
Matt
Matt, No errors. I retried the last snippet, it no longer prevented the list from displaying, but didn't solve the problem. Thanks Again.
Eric Hunt
Welp, Eric, it sounds like defsales is not defined when it gets called. I copied your code above and it worked for me. Try using alert(defsales) to see what it's value is at the beginning of your function.
Matt
Matt, thanks, your right, defiles is undefined, but I cant figure out why, or how to define it any better. Do you have any ideas why it worked for you? Please forgive my ignorance.
Eric Hunt
Btw, thx for the alert tip. I never used that before. It's pretty cool.
Eric Hunt
From the sounds of it what ever is calling your function isn't passing it as a parameter. If you're using Firefox there is a great plugin called FireBug that lets you walk though your code as it executes, it's easier then debugging with the alert() statement ;)
Matt
Thx Matt, I think I know what is passing the parameter, and I'll have to wait until I get to the office to get at the unscrambled ruby. Thanks for all your help.
Eric Hunt
np, mind marking your question as answered?
Matt
I have already accepted this answer yesterday.
Eric Hunt