Jump to content

radio selector


funbinod

Recommended Posts

I've some problem executing some line with different condition on radio button selection...

	$('#dname').keypress(function(e) {		if (e.which === 13) {	    		if ($('#by').val()=='CASH') {				$('#cqd').select();			} else if ($('#by').val()=='CHQ') {				$('#ref').select();			}		}	});

in this code #by is radio button with two options - val='CASH' & val='CHQ'. on 'keypress', if CASH is selected '#cqd' should be selected and if CHQ is selected '#ref' should be selected. but this only the #cqd is selected in both conditions. please guide me to execute it properly.

 

thanks in advance...

 

Link to comment
Share on other sites

BY: <input type="radio" name='by' id='by' value="CASH" checked />CASH | <input type="radio" name='by' id='by' value="CHQ" />CHEQUE

sorry! I might have been misunderstood...

 

please guide how can I have options for radio buttons for one question???

Edited by funbinod
Link to comment
Share on other sites

You can't have more than one id attribute with same value, MUST be Unique, by just having one it will retrieve the last element value, which is what you are getting. give the second id of id="by2", and reference that, the identical name attribute value is fine, but id MUST be unique within a page.

Link to comment
Share on other sites

oh thank u.

now I changed the id for both options to 1 and 2 and now I changed the jquery selector by its name...

	$('#dname').keypress(function(e) {		if (e.which === 13) {		    	if ($('input[name=by]').val()=='CHQ') {				$('#cqd').select();			} else if ($('input[name=by]').val()=='CASH') {				$('#ref').select();			}		}	});

selecting by its name also not doing the proper job :( . what do u suggest me to make this done...????

Link to comment
Share on other sites

The thing with radio buttons is that, in Javascript, each radio button is completely independent from every other.

 

You need to go through all the radio buttons looking at their "checked" property to see which one is selected.

 

jQuery has a :checked selector, so you could do it this way:

if ($('input[name=by]:checked').attr("value") == 'CHQ') 
  • Like 1
Link to comment
Share on other sites

I figured the val() method only was for elements whose value can change. I don't know what jQuery does internally, so I use a method that I can be guaranteed will work. If val() works than go ahead and use it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...