JQuery Question on Radio Buttons text
I have a few Radio buttons, some of which have to have the same value. I need to know which one the user have selected, so I am using text, however it does not work well so far.
Example:
I have 4 Radio buttons: Apple, Pear, Lemon and Orange, with values: Apple, Pear have Value 1, and Lemon and Orange have Value of 2
(this unfortunatelly cannot be changed for this particular application)
I need to figure out that the user picked specifically Apple, so I am using text as follows:
var FruitSelected=$('[name='Nameoftheradiobuttonhere]:checked').parent().text();
alert (FruitSelected) - gives me proper text, which user selected - Apple. When I pick other fruits - it also gives me proper text for the corresponding fruit.
However, when I run a simple
if (FruitSelected)='Apple' { Do something} - it is always true, regardless of what I picked.
When I use if (FruitSelected)==='Apple' - it is always false, regardless of what I pick.
Please advise and sorry if this is a simple question.
Comments
@Alex Kogan is this just a matter of = vs. ==? (The worst thing when switching between ObjectScript and JS...)
Specifically, did you mean:
if (FruitSelected=='Apple') { /*do something*/ }It was a whitespace issue - needed trim().
It was a whitespace issue - needed trim().
The solution was adding .trim() to the end, so: FruitSelected=$('[name='Nameoftheradiobuttonhere]:checked').parent().text().trim(); Then everything works as it should. It appears that there was some char, which would not allow the true/false to work properly, while displaying correctly in alerts and trim() takes them all out. Thank you Tim!