Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to send variables value?

    I need to add onclick event to an element => myFunction(myVariable) but instead of the myVariable I need it's value to be set as an attr. As in:
    Code:
    <!html>
    <head>
    </head>
    <body>
    <input type="button" id="element" value="click me!">
    <script type="text/javascript">
    var myVariable = "myvalue";
    document.getElementById('element').onclick = function () {
      showIt(myVariable); 
    }
    myVariable = "myvalue2";
    
    function showIt(msg){
    alert(msg);
    }
    </script>
    </body>
    </html>
    Now i need the "click me!" input to alert "myvalue" and not "myvalue2".

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    In other words, you want it to alert the value of myVariable at the time the onclick was set up, not whatever value it gets later, right?

    The easiest way to do this would be to simply copy it to another variable that you don't use for any other purpose and alert that, instead.

    But, yes, it can be done. It requires a closure to make it work.
    Code:
    var myVariable = "myvalue";
    
    function setUpOnClick(withThis)
    {
        var handler = function () { showIt(withThis); };
        return handler;
    }
    document.getElementById('element').onclick = setUpOnClick(myVariable);
    
    myVariable = "something else and it won't affect the onclick's message";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <input type="button" id="element" value="click me!">
    <script type="text/javascript">
    var myVariable;
    document.getElementById('element').onclick = function () {
    myVariable = "myvalue";
    showIt(myVariable); 
    }
    myVariable = "myvalue2";
    
    function showIt(msg){
    alert(msg);
    }
    </script>

    Quizmaster: In medicine, a cochlea implant is designed to stimulate which of the five senses?
    Contestant: Could you repeat the question, please?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks like I oversimplified the example. None of these will work for me
    This code (in reality it's much more complex...hope i didn't mess it up simplifying it again) will better explain what I need:
    Code:
    for(var i = 0; i < x; i++) {
      document.getElementById('field-' + i).onchange = function () { myFunction(i); }
    }

  • #5
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    As Old Pedant suggested, you may get a solution by using a closure
    Code:
    for(var i = 0; i < x; i++) {
      document.getElementById('field-' + i).onchange = (function (index) {
        return function() { myFunction(index); };
      })(i);
    }
    I am still learning English

  • #6
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thats it! Thank you.

    Quote Originally Posted by Amphiluke View Post
    As Old Pedant suggested, you may get a solution by using a closure
    Code:
    for(var i = 0; i < x; i++) {
      document.getElementById('field-' + i).onchange = (function (index) {
        return function() { myFunction(index); };
      })(i);
    }

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot too.It helps me lot too.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •