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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    small problem changing a onclick

    here is what i have working
    Code:
    document.getElementById("myid").onclick = sale1;
    that works fine but i need to have the "1" as a variable like

    Code:
    document.getElementById("myid").onclick = sale+count5;
    sale being the name and count5 being the variable containg numbers.
    i need to combine the sale with the variable and change the onclick to that.
    any ideas

  • #2
    New Coder
    Join Date
    Jun 2012
    Location
    Atlanta, Georgia, United States
    Posts
    34
    Thanks
    0
    Thanked 8 Times in 8 Posts
    There is apparently some confusion here. You are trying to assign variables as click handlers. This is how your code should look:

    Code:
    document.getElementById("myid").onclick = function(){
        /* Do something when #myid is clicked */
    };
    For instance, if you wanted to change the value:

    Code:
    ​document.getElementById("myid").onclick = function(){
        this.value = parseInt( this.value, 10 ) + 5;
    };​​​​
    This would increment the current value by 5 when the element is clicked.

    See it in action: http://jsfiddle.net/Baa8f/

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    what i need to do is change what the onclick event does.
    i need it to change a div onclick from sale1() to whatever is in the variable.
    ranging from sale1 to sale33.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    document.getElementById("myid").onclick = eval("sale"+count5);

    anytime you use eval, there is a better way
    Last edited by DaveyErwin; 06-27-2012 at 09:01 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Is this what you mean?

    Code:
    <div id = "myid">Something here</div>
    
    <script type = "text/javascript">
    
    var count5 = 1;  // a number determined by ?
    var sale = { 
        sale1: function() {alert ("function sale1 activated") }, 
        sale2: function() {alert ("function sale2 activated") }, 
        sale33: function() {alert ("function sale33 activated") } 
    }; 
    
    document.getElementById("myid").onclick = function(){
    sale['sale' + count5](); 
    };
    
    </script>
    You will see that the functions are stored in an object. You could also access the functions directly from the window object but this has the disadvantage of using the global namespace, which many consider should be avoided.

    Code:
    document.getElementById("myid").onclick = function(){
    window['sale' + count5](); 
    };



    Following protracted negotiations we have come to an arrangement with the banks under which we have agreed not to supply goods to our customers on credit. For their part the banks have agreed not to sell fishing tackle.
    Last edited by Philip M; 06-27-2012 at 09:18 PM. Reason: Noticed typo

    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.


  •  

    Posting Permissions

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