views:

76

answers:

7

Hi, Using jquery,

The following line works i.e. radioClicks function gets called.

$("input:[name='"+54464+"']").bind( "click", radioClicks ); 

but this one doesn't:

$("input:[name='"+options.rowId+"']").bind( "click", radioClicks );

and yes, you have guessed it, options.rowId = 54464 (at least in the debugger) .

What am I missing ???

Thanks

EDIT:

I removed the : as some suggested, I used alert(options.rowId) and it shows 54464 as expected. Also, it is not used in a loop. The code is:

function radioFormatter (cellvalue, options, rowObject)
{
    $("input[name='"+options.rowId+"']").bind( "click", radioClicks ); 

    if("checked" == cellvalue)
        return "<input type='radio' name='"+ name +"' id='"
               + options.colModel.name + "' value='" + options.rowId
               + "' checked>";

    return "<input type='radio' name='" + name + "' id='" +options.colModel.name
           +"' value='" + options.rowId + "'>";
}

It is used with jqgrid where I have a row with multiple columns with a radio button in it.

I have tried everything I can think of with no success...

Thanks

+1  A: 

I'm not sure why the first works actually, but the : is extra in there, it should just be this:

$("input[name='"+options.rowId+"']").bind( "click", radioClicks );

The : usually preceeds pseudo-classes or filters, but it's not needed for your attribute-equals selector.


Edit: As for the difference, are your sure options.rowId == 54464 at that time? Stick a console.log(options.rowId) just before that line and see what the result in Chrome or Firebug is.

Nick Craver
Works [for me](http://jsfiddle.net/).
Matthew Flaschen
@Matthew - It's not a correct selector, not sure how you can argue this...just because it doesn't *break* doesn't mean it's *valid*.
Nick Craver
@Matthew - `:input` *is* a valid selector I completely agree, but that's not what he's using, he has `input:`.
Nick Craver
@Nick, I misread it. However, regardless of whether this syntax is a bug in jQuery or the OP's code, I still don't see how it's causing the inconsistency between the two lines.
Matthew Flaschen
I just tested jQuery 1.4.2 and it works with or without the `:` between `input` and `[`.
gradbot
@Matthew - I agree that's probably not the main issue, I don't think that value is what's posted *when the selectors hit* , I added an edit to address this, hopefully that's the issue.
Nick Craver
@gradbot - As I said it's not *valid*, it *works*, lots of things work, even in HTML, but it's not in the spec, it's not intentional, and it's libel if not likely to break in any edge cases or future releases (of Sizzle in this case).
Nick Craver
How are you debugging this? You have to be careful of references in your debug statements. If you just do console.log(options) the value in the bebugger could change for options.rowId. Try doing an alert(options.rowId) before that bind call.
gradbot
@Nick - I was just validating that it works with or without. I'm surprised it works at all with the colon in there.
gradbot
A: 

It looks like you have a double quote.
Try this:

$("input:[name="+options.rowId+"]").bind( "click", radioClicks );  

It seems to be working. http://jsfiddle.net/EG2Yx/. Check your other codes?

Chase
The OP's quotes are fine. The inner single quotes are allowed, so the final result of the concatenation is `input:[name='54464']`.
patrick dw
A: 

My guess would be options.rowId returns a int however the $("input[name='"+options.rowId+"']") requires a string

try

var idname = options.rowId;
$("input:[name='"+idname+"']").bind( "click", radioClicks );

or use firebug or chrome js debug to findout more

D.J
An `int` works fine in string concatenation. It will be automatically converted into a string with the rest of it.
patrick dw
yep, maybe you are right. i did not test the code myself. however, at least, by coding the code above, it would be easier for you to set break point in debug to see whether the variable is correctly assigned or not. good luck!
D.J
+1  A: 

I think the selector is correct. But without whole code, it's hard to tell the root cause.

My guess is that the code uses a loop to attach event listeners to a group of radio buttons and the author meets the infamous closure issue with loops. options.rowId always refers to the last value in the loop.

Anyway, need more details.

Alex Cheng
Even in that situation, since options.rowId is only used in the selector, it shouldn't matter. It's being evaluated when $() is called, not when radioClicks is called.
Ryan Kinal
A: 

Aside from the colon being in the wrong spot this code is fine. I think your debugger is fooling you.

You have to be careful of references in your debug statements. If you just do console.log(options) the value in the debugger could change for options.rowId. Try doing an alert(options.rowId) before that bind call.

alert("input[name='"+options.rowId+"']");
$("input[name='"+options.rowId+"']").bind( "click", radioClicks );
gradbot
A: 

the quoting around [attribute=] param is not strictly necessary, so

$("input[name=" + options.rowId + "]").bind( "click", radioClicks )

;

Scott Evernden
Interesting, I didn't know the quotes were optional.
gradbot
here: .. http://api.jquery.com/attribute-equals-selector/ .. says 'Quotes are optional'
Scott Evernden
A: 

I have found the problem but don't know how to fix it:

This doesnt work:

var options = {rowId:1223}; $("input:[name="+options.rowId]+"]").bind( "click", function(){ alert('clickedz'); });

But this does:

var options 1223; $("input:[name="+options]+"]").bind( "click", function(){ alert('clickedz'); });

I am not a js expert but the issue seems to be how the option object gets created.

stef