views:

40

answers:

3

I'm using jquery ajax. The server returns a JSON object and I am fetching the array. Everything works fine in FF, of course, but somehow when i try to use this syntax:

$('#edituserLink').attr("onClick","edit('"+user[0]['USER_ID']+"');");

the onClick function wont have the value as an argument. Why?

A: 

Use JQuery Events to do that (not the onclick attribute (case matters))...

ircmaxell
+1  A: 

Why aren't you using the standard .bind() method?

$('#edituserLink').bind('click', function() { edit(user[0]['USER_ID']); });

Some people prefer the shorthand form:

$('#edituserLink').click(function() { edit(user[0]['USER_ID']); });

but personally I regard that as a bad style. (Does the concept bad style even exist!? Contradictio in terminis!)

MvanGeest
i tried your solution and my event is empty:<a style="" onclick="edit('');" href="#" id="edituserLink">Edit User</a>
Kel
That depends on the availability of `user` at the time you bind this... if it's not available, there's a small problem. Closures. Is `user` a global variable? Then you can use `window.user` instead of `user`.
MvanGeest
the element is always available. i just hide it and show it on occasions.
Kel
No, I mean the availability of the value of the **variable (array)** `user`. I can create a live example if you want to.
MvanGeest
sorry for my late reply, i just came back from late lunch. sure i would appreciate a live example.
Kel
There you go, this is an example that works: http://jsfiddle.net/RNhcF/. Would you also like an example of a situation in which it doesn't work?
MvanGeest
no i am all set. thank you very much.
Kel
A: 

Instead of using the onClick event, use the jQuery click() event which is fully cross-browser compatible:

$('#edituserLink').click(function() {
     edit(user[0]['USER_ID']);
});
Nate Dudek