I keep getting the error:
Timestamp: 26/04/2012 11:55:14
Error: togglebox is not defined
Line: 1
Anyone?
Thanks in advance
http://codex.ww.wp.xz.cn/Function_Reference/wp_enqueue_script#Load_a_script_from_your_theme_which_depends_upon_a_WordPress_Script
remove wp_enqueue_script("jquery"); from the header.
Put your script in a file and call it togglebox.js. Create a folder “js” in your theme folder and put togglebox.js in it. And put this in your theme’s functions.php
<?php
function my_scripts_method() {
wp_register_script( 'togglebox', get_template_directory_uri() . '/js/togglebox.js', array('jquery'));
wp_enqueue_script('togglebox');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>
This way jquery and togglebox.js are called in the header.
When using jQuery you also can use toggle(): http://api.jquery.com/toggle/
Thankyou so much for your reply! This is slowly driving me mad!
Now I have this in the funcions php:
function my_scripts_method() {
wp_register_script( 'togglebox', get_template_directory_uri() . '/js/togglebox.js', array('jquery'));
wp_enqueue_script('togglebox');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
Nothing in my header.php
And this in my page:
<a onclick="visibilityToggle('sandwell');" href="" style="color:black;">sandwell</a>
<div id="sandwell">
test
</div>
I get the error:
Error: visibilityToggle is not defined
Source File: http://www.blackcountry.nhs.uk/beta/about-us/
Line: 1
Can you please help?
Many thanks, really appreciate it.
Well I see jQuery and togglebox.js in your header.
try it with this in your togglebox.js:
jQuery(document).ready( function($) {
var paragraph = $("<p>");
var link = $('<a id="toggle_sandwell">Sandwell</a>');
paragraph.append(link);
$(".entry-content").prepend(paragraph);
$(link).click(function (e) {
e.preventDefault();
$("#sandwell").toggle();
});
});
and remove your link from your page template file. This way when people are browsing with javascript turned off they don’t get to see the link.
If you need this only on that page you can use is_page() in the first enqueue code
if(is_page(2)){
wp_enqueue_script('togglebox');
}
Thanks very much for your reply.
I’d like to pass the id of the element to the function and use this parameter to toggle the hide show, so I can toggle any element in the page using the same function.
Can I just use some really simple Jquery like this?
function togglebox(id){
var e = document.getElementById(id);
if (e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
Thanks so much for your help
Ok, when showing a link to toggle content format it like so:
<div class="toggle">
<a class="toggle-link" href="#">toggle</a>
<div class="toggle-container">
<!-- container that gets toggled -->
<?php the_content(); ?>
</div>
</div>
Put this in your theme’s stylesheet to hide the toggle links by default:
.toggle-link {
display: none;
}
And use this in togglebox.js to toggle multiple elements:
jQuery(document).ready( function($) {
$("a.toggle-link").show();
$("a.toggle-link").click(function (e) {
e.preventDefault();
$(this).next(".toggle-container").toggle();
});
});
if you have the toggle link inside another element (<p>,<h2>, etc) you need to change this:
$(this).next(".toggle-container").toggle();
to this:
$(this).parents('.toggle').children(".toggle-container").toggle();
or this:
$(this).parent().next(".toggle-container").toggle();
Thanks very much for your help