How do I add multiple instances of Javascript on one page?

3 replies
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
#add #instances #javascript #multiple #page
  • Profile picture of the author dwbiz05
    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
    Signature

    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

    {{ DiscussionBoard.errors[9558461].message }}
  • {{ DiscussionBoard.errors[9558849].message }}
  • Profile picture of the author kewkii
    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.
    {{ DiscussionBoard.errors[9558911].message }}

Trending Topics