simon
simon

Reputation: 6117

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());
    });     
});

Upvotes: 4

Views: 9830

Answers (6)

Nick Brunt
Nick Brunt

Reputation: 10067

Lose the @ symbol in the selector.

Upvotes: 0

mr.freeze
mr.freeze

Reputation: 14050

You need quotes around the attribute value, 'login'. Also, I don't think you need the @ symbol:

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

Upvotes: 0

Erick Petrucelli
Erick Petrucelli

Reputation: 14902

Remove @ symbol from your selectors, since it doesn't exists on jQuery 1.5.2.

Here is your Fiddle updated.

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237985

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.

Upvotes: 10

Robert
Robert

Reputation: 21388

Just $("input[name=login]") works for me. Never seen the @ used in that context before, is it supposed to be doing something?

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359966

Remove the @. $("input[name=login]")

You probably also want to use this in the callback:

$(document).ready(function() {  
    $("input[name=login]").change(function(){
      alert($(this).val());
    });     
});

Upvotes: 1

Related Questions