views:

358

answers:

3

consider this code sample :

<a href="some url" onclick="confirm('ok to proceed ?')">bla</a>`

<script type="text/javascript">
    $(document).ready(function() {
              $("a").live("click", function (event) {
                    // In that function, I want to get 
                    // the "confirm" return value                  
               });
</script>

It is possible to get this return value without modifying the DOM ??

Thanks.............

+1  A: 

you need to explicitly add the word "return", like this:

onclick="return confirm('ok to proceed?')"

EDIT: my initial reply was BS, here's what I got to work finally.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
$(document).ready(function(){
    $("a").click(function() {
        var stuff = confirm('ok?');
        if (stuff) {
        alert('was ok, stuff=' + stuff);
        }
    else {
        alert('was not ok, stuff=' + stuff);
        }
    });
});
    </script>
  </head>
  <body>
    <a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;
  </body>
</html>
Nathan Hughes
Hmm maybe this is it, too. Where does the return value get stored? The jQuery `event` parameter passed to the anonymous function?
Marc W
iirc the event gets cancelled if the user clicks cancel, so I'm not sure the JQuery code would execute.
Nathan Hughes
+2  A: 
<a href="some url" onclick="var myVar = confirm('ok to proceed ?')">bla</a>

If I remember correctly, that should work. You would then have access to myVar in your jQuery block like you would have access to any other Javascript variable.

Marc W
+2  A: 

No, the value is not stored anywhere for you to access. The only way to get that value is to move it to a jQuery handled click event like this:

$(document).ready(function() {
    // The removeAttr removes the original "onclick" attribute
    $("a").removeAttr('onclick').live("click", function (event) {
       var ret = confirm('ok to proceed ?');
       // Do what you want here because `ret` has the return 
       // value from the `confirm` call.
    });
});
Doug Neiner