0

First of all excuse for my weak english, I try to use the following javascript code and I want to return id variable and use it in other function, but it seem does not work correctly and does not return it, can someone help me out writting this

var idcb = $('.box').click(function() {
    var id = $(this).attr('id');
    return id;
});

I want to use var idcb in this function :

$(".hs").click(function() {    
    $(idhs).slideToggle("slow");
        return false;
    });
});
0

8 Answers 8

2

for that to work, the jquery click implementation would need to know that the function you pass it returns a value, and that it itself should return that value - which isn't the case.

instead, you could use some closure magic to do this easily. try this:

var idcb;
$('.box').click(function() {
    idcb = $(this).attr('id');
});
Sign up to request clarification or add additional context in comments.

2 Comments

your code works good , and i had made a mistake, but there is another problem, when i click the variable would be set, but should click another time,to work correct, how could hide function and click fuction excute continuesly?
you could call one from the other. i'm not entirely sure that is what you want though as your question was somewhat unclear...
1

You are passing an anonymous function to an event handler, you cannot return a value from this type of function. The solution is to use closures to get around this :

var idcb = null;
$('.box').click(function() {
    idcb = $(this).attr('id');
});

The variable idcb will always be set to the id of the last .box that was clicked.

Comments

0

The function is given as a parameter to the click function and is run asynchronously when the user clicks on an element. So you can't return a value directly, since the function won't be run immediately. You can look up the id value another way, or pass it to a function to work with instead of returning it, or just do this:

var idcb = $('.box').click(function() {

   var id = $(this).attr('id');

   $(id).slideToggle("slow");
});

Comments

0

You cannot do it the way you are trying to. However, what you can do is use a variable that is accessible by both functions to share values between them.

var icdb = 'test';

function a() {
    icdb = 'another value';
}

function b() {
    console.log(icdb);
}

a();
b();

You could also call b from a and pass the variable as argument:

function a() {
    b('test');
}

function b(icdb) {
    console.log(icdb);
}

a();

Comments

0

your variable idcb contains the click handler !

you need to declare the vriable outside the handler and assign to it inside to make this work:

 var idcb = null;

 // ... whatever
 $('.box').click(function() {
     idcb = $(this).attr('id');

     return true; // return 'true' from an event handler to indicate successful completion
 });

Comments

0

Check this out

var id=null;
$('.box').click(function() {
    id = $(this).attr("id");
});

$(".hs").click(function() {
    $("#"+id).slideToggle("slow");
    return false;
});

4 Comments

i used your code, but it didn't work, i put an alert function in the first function ,it shows the true value but when i put alert outside it, it shows null value, How can i fix that?
You have a click event, so you need to click on .box before you sets the value.
If you just want to get the id of .box then can you do $(".box").attr("id"); Can you post your code at jsfiddle so I can se what you trying to do?
here is url project, when you click in close icon, box hides, but should click the close icon 2 times, how could use it with 1 time click?
0

You could pass in the an object as a reference to the click function and change the value from the callback. Another option is using closures The caveat to this jsfiddle is that you have to call a .box element first.

http://jsfiddle.net/D6B73/2/

var idcb = {};
$('.box').click(idcb, function (event) {
        event.data.value = $(this).attr('id');
        console.log(idcb.value);
 });

$('.hs').click(function() {    
   $('#'+idbc.value).slideToggle("slow");  
});

3 Comments

thnx, it's good but i want to run 2 function continuesly, mean first get id for .box div and immediately run .hs function and slide it's section,how?
do you really need the .hs call? Couldn't you just do the sliding inside the .box click? You could do something like this. At that point though you wouldn't beed the idcb variable though,
here is the url: itsun.ir/test,, my goal is when a user clicks on close icon, the .box div hide, .hs is just handle hiding .box div, do you have a better idea to do this?
-1

You can do it this way:

$(".hs").on('click', function() {
   var id = $('.box').attr('id');
   alert(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.