Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-27-2012, 05:03 PM   PM User | #1
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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
gerble1000 is offline   Reply With Quote
Old 06-27-2012, 07:56 PM   PM User | #2
jonathansampson
New Coder

 
Join Date: Jun 2012
Location: Atlanta, Georgia, United States
Posts: 34
Thanks: 0
Thanked 8 Times in 8 Posts
jonathansampson is an unknown quantity at this point
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/
jonathansampson is offline   Reply With Quote
Old 06-27-2012, 08:38 PM   PM User | #3
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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.
gerble1000 is offline   Reply With Quote
Old 06-27-2012, 08:58 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Old 06-27-2012, 09:02 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 06-27-2012 at 09:18 PM.. Reason: Noticed typo
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:02 AM.


Advertisement
Log in to turn off these ads.