views:

158

answers:

3

Hello all,

New to SO and javascript. I have been playing with JQuery for a couple of days now and I have a problem that I can't seem to solve. I have absolutely no js experience at all so if someone could help me out by literally spelling out what the issue is, I'd be gratful.

I have a single group of radio buttons that will determine if a user is an administrator or a user. I probably have my html messed up also but what I need is the current ticked value so I can save it to a database. If the user is an administrator, the value will be 1 and if he is a user, the val will be 0.

Here is my html:

<tr>
<td class="admMarker"><label for="admEmpIsAdmin">Admin</label>&nbsp;<input type="radio" id="admEmpIsAdmin" name="status" value="1" checked="checked" class="admChkbx"></td>
<td class="admMarker"><label for="admEmpIsUser">User</label>&nbsp;<input type="radio" id="admEmpIsUser" name="status" value="0" class="admChkbx"></td>
</tr>

Here is the javascript:

var admEmpIsAdmin = $("input#admEmpIsAdmin").val();

Can someone please tell me what I am doing wrong here? This code outputs the POST vals but I would also like to know if anyone has any suggestion for me on how to display the current status from the values when they come out of the database.

Thanks!

+4  A: 

Try this:

$('input[name=status]:checked').val()
Brian
Cool.. Very cool! Thank you. FYI: Both of your solutions produced correct results. The first one outputs "true" and "false" whereas the latter solution gives me the actual values and is what I need. Thank you all!. I'l be posting again for another problem. Thanks again guys!
You can also use $('.admChkbx:checked') since you have that class assigned to both.
bendewey
I have another question and am not sure whether I should start a new topic or not. It's related to the same issue I am having here. The correct vals are now being saved in the db but when I use AJAX to pull the vals into the form, it isn't working. Any ideas? New post?
Correction.. I'm using JSON to pull the vals into the form.
A: 

Try:

var admEmpIsAdmin = $("input#admEmpIsAdmin").is(":checked");
bendewey
+1  A: 

You can use something like

var value = null;
for(i=0; i<formName.status.length; i++)
    if(form.status[i].checked)
      value = form.status[i].value;

Where status is the name of your input (radio) and formName the name of your form (not present in the code).

EDIT: I have edited the answer in order to be correct, because just form.status.value would not have worked (the inputs are radios). And I'm pretty much sure it works in all browsers.

andi
I'm not sure this works with all browsers...
bendewey