What is the best way to pass the var full to function b. I don\'t want to use global variables. Is return the only option.
argument? is there something im missing here?
function b(x){ alert(x); };
call with
b(full);
Unless you dont want function b to exist, or used outside outside of the click function, i would move it outside the anonymous function. And then just specify the arguments in the definition then just pass them like normal
Example
<script type="text/javascript">
$("body").click(function(e){
var name = $(e.target)[0].nodeName;
var nameid = $(e.target)[0].id;
var classname = $(name+"#"+nameid).attr('class');
var full = name+"#"+nameid;
console.log(nameid);
b(full);
});
function b(full)
{
alert(full);
};
</script>
You can pass a variable to a function by defining it as follows:
function b(x) {
alert(x);
}
and then calling it by stating its name and passing a variable as the argument:
b(full);
So in the context of your code:
$("body").click(function(e){
var name = $(e.target)[0].nodeName;
var nameid = $(e.target)[0].id;
var classname = $(name+"#"+nameid).attr('class');
var full = name+"#"+nameid;
console.log(nameid);
function b(x){
alert(x);
};
b(full);
});
// define b
function b(full) {
alert(full);
}
Actually, you aren't using global variables. You're defining them inside the JQuery anonymous function bound to the body element.