funbinod Posted June 20, 2015 Share Posted June 20, 2015 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 More sharing options...
dsonesuk Posted June 20, 2015 Share Posted June 20, 2015 How can a single radio button, with unique id ref of '#by' have two values? Link to comment Share on other sites More sharing options...
funbinod Posted June 20, 2015 Author Share Posted June 20, 2015 (edited) 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 June 20, 2015 by funbinod Link to comment Share on other sites More sharing options...
dsonesuk Posted June 20, 2015 Share Posted June 20, 2015 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 More sharing options...
funbinod Posted June 21, 2015 Author Share Posted June 21, 2015 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 More sharing options...
Ingolme Posted June 21, 2015 Share Posted June 21, 2015 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') 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted June 21, 2015 Share Posted June 21, 2015 use .val() instead of .attr("value") 1 Link to comment Share on other sites More sharing options...
Ingolme Posted June 21, 2015 Share Posted June 21, 2015 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 More sharing options...
dsonesuk Posted June 21, 2015 Share Posted June 21, 2015 Its primary use is to get a value from an input, secondary you can apply value to an input by placing the value between elliptical brackets. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now