Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Finding radio button by its value

Tags:

jquery

I'm scratching my head over this one.

HTML:

<form>
      <input name="changes" type="radio" value="2012-05-18T16%3A53%3A00.000000000000%2B0100">
      <span>2012-05-18 16:53:00 +0100</span>
      <input name="changes" type="radio" value="2012-05-29T16%3A51%3A00.000000000000%2B0100">
      <span>2012-05-29 16:51:00 +0100</span>
      <input name="changes" type="radio" value="2012-05-31T17%3A30%3A00.000000000000%2B0100">
      <span>2012-05-31 17:30:00 +0100</span>
      <input name="changes" type="radio" value="2012-06-01T16%3A30%3A00.000000000000%2B0100">
      <input id="view-change" type="button" value="View">
</form>

In the Chrome JavaScript console:

var today = "2012-05-18T16%3A53%3A00.000000000000%2B0100"
$('input[value="2012-05-18T16%3A53%3A00.000000000000%2B0100"]') -> Finds object
$('input[value=today]') -> Nothing

Huh? Why can't i select the input using a var?

PS. Ive confirmed, jQuery.type(today) = string

like image 920
rwb Avatar asked Dec 31 '25 03:12

rwb


2 Answers

Try:

$('input[value="'+today+'"]')

See jsfiddle

like image 159
khomyakoshka Avatar answered Jan 02 '26 16:01

khomyakoshka


You should do that like this:

$('input[value="' + today + '"]');

In php you can just insert vars into a string and they will be substituted with their values, but not in javascript. You need to concatenate the string part with the contents of the variable to make it work. Now you are just looking for an input with a value of (literally) 'today', not the contents of the var 'today'.

like image 42
Asciiom Avatar answered Jan 02 '26 15:01

Asciiom