2

Ok, so this is probably obvious.

I need to get the ID of a clicked div:

$("div.editable").click(function(e) {  
  var editid = $(this).attr("id");  
});  

And the use that ID in a function w/parameters:

ajaxStyle(value, 2, editid)

But it doesn't work when I write it like this. It either returns "undefined" or just doesn't work.

0

2 Answers 2

5
var editid;
$("div.editable").click(function(e) {
   editid = $(this).attr("id");
}); 

It's all about function scope.

Sign up to request clarification or add additional context in comments.

1 Comment

wont let me accept yours as correct yet. Will do when I can :)
1

You are declaring your editid variable within a function, so it is only visible within it and not defined outside of it.

This will work (though will pollute the global namespace):

var editid;
$("div.editable").click(function(e) {  
  editid = $(this).attr("id");  
});  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.