views:

54

answers:

2

On Safari 4.0, I have a check that prints "????" if hid === "#but0_0"

Works:

$("#but0_0").attr("onmousedown",function(){alert("Aha!");});

Doesn't work:

var id = "but"+y+"_"+x;
var hid = "#"+id;
if (hid === "#but0_0") console.debug("????");
$(hid).attr("onmousedown",function(){alert("Aha!");});

The console shows "????" so the code is getting hit and the variable is "the same" (from === string compare point of view) as the string literal but apparently jQuery's $() operator knows the difference?

I have 30 divs to manipulate and I need to compute the id of the div at run time. Any ideas? It's probably something simple like pulling out a "string" from a "String" or some such JavaScript stupidity.

+2  A: 

Give the following a shot:

$(hid).live('mousedown', function() {alert("Aha!");});

if your elements are created dynamically.

Mark
Your comment below did the trick: since the elements were created dynamically I needed to use elem.live("onmousedown",...)
Jared Updike
I updated my answer to include the .live events
Mark
+1  A: 

Strange that that's the case.

Why aren't you using $(hid).bind('mousedown', function () { /* something */ }); to attach your events? (http://api.jquery.com/bind)

Matt
I agree about binding his events. You could also use $(hid).live('mousedown', function() {}); if your elements are created dynamically.
Mark
@Mark: if you add this as a separate answer I can mark it as the accepted answer.
Jared Updike
Updated my previous answer with .live if you'd like to accept that :)
Mark