Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 27th Sep 2014, 08:02 AM   #1
Membership Site, SEO Guy
War Room Member
 
vtotheyouknow's Avatar
 
Join Date: 2010
Location: Bangkok, Thailand
Posts: 697
Thanks: 198
Thanked 258 Times in 165 Posts
Blog Entries: 1
Default
How do I add multiple instances of Javascript on one page?
Share on: 
fb share twitter share gplus share more share

Hi All,

I'm attempting to put several instances of Javascript code on the same page but it isn't working.

Here is the code that goes in the header...

Code:
<script type="text/javascript">
function toggleAccept() {
var acceptLink = document.getElementById("accept");
var agreeCheckbox = document.getElementById("agreeCheckbox");
if (agreeCheckbox.checked) {
acceptLink.onclick=function() {
window.location=this.href + "&cbrblaccpt=true";
return false;
}
} else {
acceptLink.onclick=function() {
mustAccept();
return false;
}
}
}
function mustAccept() {
window.alert("By clicking accept, you agree to the payment terms of this recurring product.");
}
cbrblaccpt

</script>
...and here is the HTML:

Code:
<p align="center"><form action="#"><input type="checkbox" onchange="toggleAccept()" id="agreeCheckbox">
<label for="agreeCheckbox"><b>I understand that this product is billed on a monthly basis and that I can cancel my subscription at any time.</b></label></form></p>
                                        	<p align="center"><a href="http://2-monthly.vicdorfman.pay.clickbank.net/?cbur=a" id="accept" onclick="mustAccept(); return false;"><img alt="" src="images/signup-37.jpg" style="border: 0px solid ; width: 350px; height: 147px;" /></a></p>
If I try to place the same HTML snippet a second or third time on the page, the function no longer works correctly.

Please help!

Thanks,
Vic

vtotheyouknow is offline  
Unread 28th Sep 2014, 10:35 PM   #2
Coder of the Web
War Room Member
 
Join Date: 2010
Location: Kansas City, MO - United States
Posts: 75
Thanks: 2
Thanked 40 Times in 9 Posts
Default
Re: How do I add multiple instances of Javascript on one page?
Share on: 
fb share twitter share gplus share more share

HTML Code:
var acceptLink = document.getElementById("accept");
var agreeCheckbox = document.getElementById("agreeCheckbox");
My guess without seeing all the code is that you aren't changing the ID's for each of the calls.

If you keep the same id's then it will only work on the first items with those id's.

Try changing the id's for the 2nd and 3rd items you are trying to make calls on and see if that fixes the issue.

Dave

Tired of fighting with WordPress to build the site you want? You shouldn't have to spend hours tweaking your website to bend to your will. Take the next step in your journey to WordPress mastery with wordpress4anyone.com
dwbiz05 is offline  
Unread 29th Sep 2014, 02:11 AM   #3
Membership Site, SEO Guy
War Room Member
 
vtotheyouknow's Avatar
 
Join Date: 2010
Location: Bangkok, Thailand
Posts: 697
Thanks: 198
Thanked 258 Times in 165 Posts
Blog Entries: 1
Default
Re: How do I add multiple instances of Javascript on one page?
Share on: 
fb share twitter share gplus share more share

Hey Dave I tried that but the result is that the second checkbox works but not the first checkbox.

vtotheyouknow is offline  
Unread 29th Sep 2014, 02:58 AM   #4
Active Warrior
 
Join Date: 2014
Location: UK [North Yorkshire]
Posts: 59
Thanks: 0
Thanked 9 Times in 9 Posts
Default
Re: How do I add multiple instances of Javascript on one page?
Share on: 
fb share twitter share gplus share more share

Can you post a link to the page that your having problems with or more of the html. Like dwbiz05 said it sounds like your element ids aren't unique.

kewkii is offline  
Closed Thread


Bookmarks

Tags
add, instances, javascript, multiple, page


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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 07:55 AM.