I am trying to get a variable from a javascript function, but I am having a problem getting it the variable value outside the function. The variable value can be outputted just fine inside the function. Here is the script, but how can I get the value of status and use it outside the funcion? Maybe I am doing it in the wrong way, but in the end I want the value of variable to be the value of status.

<script>
            function get_id(){              
                $('.addressClick').click(function() {
                    var status = $(this).attr('id');
                    alert(status); // Here the value is printed correctly
                    return status;
                });
            }

            var variable = get_id();
            alert(variable);        // Here the valiable isn't outputed

            $("#"+variable).confirm();
</script>

Can you create the variable outside if the function so its global? Then just change line 10...

 variable = get_id();

You can then access the variable outside of that function.

Thank you JeorgeM,
I am a begginer in Javascript so if you have some free time could you give a shoot to this code. I tried your suggestion but I could not fix my problem.

Ok, so looking at your code (sorry, was on my mobile before), your code doesnt make sense where you have on line 3 a jQuery click method within the function get_id().

On line 10, you are assigning the result of status to the variable called "variable" but status is undefined because '#addressClick' has not been clicked yet.

So, lets take a step back and can you explain what it is you are trying to do? That would be helpful so you can design code around your objective.

Basicaly I have a list with some elements generated from a database table.
At the end of each element I have a button "delete".
If the user click on it a popup window should appear to confirm the action, I know that are a dozen of examples out there that do this with the confirm function of javacript, but as you may have seen if you call that function very often in a page the browser start to block it.
I wanted to make something with not such issues.
Don't know if I am very clear?!
Hope you can help me :)

Ok so, im not seeing why you are trying to wrap this in a function. You are already handling this using the click method.

How about simply..

$('.addressClick').click(function() {
    var status = $(this).attr('id');
    $("#"+status).confirm();
});

When the element with the class of "addressClick" is clicked, you are getting that specific element's ID attribute and using that attribute in the confirm() action.

That's because your links (anchors) are acting like links.. when you click on them, they tell the browser to open that page. You need to prevent that behavior if you want something else to happen.

$('.addressClick').click(function(event) {
    event.preventDefault();
    var status = $(this).attr('id');
    alert(status);
});

Demo: http://jsfiddle.net/DwaVV/

I managed to write this example Demo http://jsfiddle.net/klausveliu/DwaVV/4/
But still it has an issue you must double click over the delete button do get the popup confirmation. Could you give it a look to the code?

I went ahead and looked at the confirm() documentation so i can get a better understanding of what that method does...
http://myclabs.github.io/jquery.confirm/

Your current issue (why you have to click multiple times) is because you are initializing the confirm method inside of the click method. This initialiation needs to be outside of the click, as a matter of fact, you dont need the jQuery click() for your anchors. All you need is one line of code for the most basic modal popup.

$('.addressClick').confirm();

See this example http://jsfiddle.net/DwaVV/5/

Wow that works now!
Thank you a lot and have a nice day :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.