Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery value of radio button: "Uncaught syntax error, unrecognized expression"

I'm trying to get the value of a radio button with $("input[@name=login]"), but am getting "Uncaught Syntax error, unrecognized expression".

See http://jsfiddle.net/fwnUm/ and here's the code in full:

<!-- Radio buttons to choose signin/register -->
<fieldset data-role="controlgroup" data-theme="z" data-type="horizontal" >
    <input type="radio" name="login" id="radio-signin" value="signin" checked="checked" />
    <label for="radio-signin">Signin</label>
    <input type="radio" name="login" id="radio-register" value="register" />
    <label for="radio-register">Register</label>
</fieldset>
$(document).ready(function() {  
    $("input[@name=login]").change(function(){
      alert($("input[@name=login]:checked").val());
    });     
});
like image 366
simon Avatar asked Dec 21 '22 14:12

simon


1 Answers

XPath-like attribute selectors were removed in jQuery 1.3. (We're now on jQuery 1.6.)

Just remove the @:

$("input[name='login']").change(function(){
  alert($("input[name='login']:checked").val());
});

Note that quote marks are also required.

See the API reference for the attribute equals selector.

like image 125
lonesomeday Avatar answered Apr 29 '23 14:04

lonesomeday