0

So I've been trying to implement this script in a WordPress site I've been working on, but it keeps throwing the error Uncaught ReferenceError: toggle_visibility is not defined and I'm not entirely sure why as it works outside of WordPress according to other people. I've had some issues before with scripts in WordPress such as the $ needing to be swapped with jQuery etc, but I've been unsuccessful so far solving this. Basically I just need it to toggle display:block/none on two divs when the corresponding links are clicked.

function toggle_visibility(id) {
 var e = document.getElementById(id);
 if(e.style.display == 'block')
 e.style.display = 'none';
 else
 e.style.display = 'block';
}
<div id="choose-membership">
 <a href="#" onclick="toggle_visability('ms-membership-wrapper-202');">Click here for free membership</a>
 <a href="#" onclick="toggle_visability('ms-membership-wrapper-213');">Click here for Paid membership</a> 
</div>
pavger
6832 gold badges12 silver badges22 bronze badges
asked Aug 19, 2016 at 10:11

2 Answers 2

4

Your onclick event has a typo:

onclick="toggle_visability('ms-membership-wrapper-202');"

Should be:

onclick="toggle_visibility('ms-membership-wrapper-202');"

Give that a go :)

answered Aug 19, 2016 at 10:15
Sign up to request clarification or add additional context in comments.

2 Comments

Ha oh dear, thats so embarrassing! Thanks for pointing that out, i clearly need more tea [caffeine] this morning!
@Moose A cup of coffee is how I start my day - we've all done it ;)
2

Misspelled function. Function is toggle_visibility you put toggle_visability in your HTML.

<div id="choose-membership">
 <a href="#" onclick="toggle_visibility('ms-membership-wrapper-202');">Click here for free membership</a>
 <a href="#" onclick="toggle_visibility('ms-membership-wrapper-213');">Click here for Paid membership</a> 
</div>
answered Aug 19, 2016 at 10:16

1 Comment

Ha oh dear, thats so embarrassing! Thanks for pointing that out, i clearly need more tea [caffeine] this morning!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.