I am upgrading a ToDo List that I made for myself a long time ago. The task names are displayed in an ordered list. I use JQuery to toggle the task description when the task name is clicked.
I am reusing the JQuery code that I used in my original ToDo List, with some changes to the class names. The code that hides the description works, but the toggle doesn’t.
<li>
<h4 class="priority-<?php echo $row->priority; ?> todo-name"><input type="checkbox" name="task[]" class="checkbox"><?php echo $row->todo_name; ?></h4>
<p class="todo-notes"><?php echo $row->todo_notes; ?></p>
<input type="hidden" name="$id" value="<?php echo $row->id; ?>">
</li>
Here is my JQuery code:
<script type="text/javascript">
$(document).ready(function() {
$('.todo-notes').hide();
$('.todo-name').click(function() {
$(this).parent().next().animate(
{'height':'toggle'},
400,
'swing'
);
});
});
</script>
and here are the js files that I have loaded: jquery-1.11.2.js and jquery.easing.1.3.js. I get no errors in the console.
I can’t see where the problem is. Can someone please tell me why this is not working?
I’m not sure why it’s not working. Maybe something to do with toggle getting deprecated in jQuery 1.9.
I think this should do what you’re after though:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Toggle demo</title>
<style>
.hidden{
display: none;
height: 100px;
background: pink;
}
h4{
cursor: pointer;
}
</style>
</head>
<body>
<ul>
<li>
<h4>Item 1</h4>
<p class="hidden">Hidden content for item 1</p>
</li>
<li>
<h4>Item 2</h4>
<p class="hidden">Hidden content for item 2</p>
</li>
<li>
<h4>Item 3</h4>
<p class="hidden">Hidden content for item 3</p>
</li>
</ul>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$('h4').on("click", function(){
var $hiddenContent = $(this).next();
if($hiddenContent.is(":visible")){
$hiddenContent.slideUp();
} else {
$hiddenContent.slideDown();
}
});
</script>
</body>
</html>
I’m trying to keep on top of everything - both server-side and client-side, but unfortunately JavaScript has been neglected because the days are just not long enough. I didn’t know that toggle was deprecated. It was a very useful feature, I thought.
Your demo works for me (Thank you), but I still can’t get my content to toggle.
In that case, can you please post enough code that I can run it on my computer and recreate your problem. Or, could you please make a JS fiddle.
I’m sure it’ll be something simple.
The reason is that there were two uses of toggle:
$(element).toggle();
This toggles the visibility of an element. If it is visible, then hide it, or if it is hidden, then show it.
$(element).toggle(function() {
do stuff
}, function() {
do stuff
});
Which would do one thing the first time the element is clicked, and another thing the second time.
This is what has been removed.
# jQuery Migrate Plugin - Warning Messages
**NOTE: This page lists the messages for jQuery Migrate 3.0. If you are using an earlier version, see the documentation on [the 1.x-stable branch](https://github.com/jquery/jquery-migrate/blob/1.x-stable/warnings.md).**
To allow developers to identify and fix compatibility issues when migrating older jQuery code, the development (uncompressed) version of the plugin generates console warning messages whenever any of its functionality is called. The messages only appear once on the console for each unique message.
**In most cases these messages are simply _warnings_; code should continue to work properly with later versions of jQuery as long as the jQuery Migrate plugin is used, but we recommend changing the code where possible to eliminate warnings so that the plugin does not need to be used.**
The production (compressed) version of the plugin does not generate warnings. To continue using jQuery code that has compatibility issues without making any changes and without console messages, simply include the production version in the file rather than the development version. See the [README](README.md) for download instructions.
All messages generated by this plugin start with the text "JQMIGRATE" for easy identification. The warning messages, causes, and remediation instructions are listed below. Items listed as **deprecated and removed** must be changed before the code will work properly without the Migrate plugin. Items listed as only **deprecated** are still supported by the current version but no longer considered a good practice, and may be removed in the future.
### JQMIGRATE: Migrate is installed, version X
### JQMIGRATE: Migrate is installed with logging active, version X
This is _not_ a warning, but a console log message the plugin shows when it first loads to indicate whether warnings will be shown on the console when appropriate. As of version 1.4.0 this message is also shown with production builds. The use jQuery Migrate in production has performance impacts and can complicate debugging as it modifies the normal behavior of the version of jQuery being used.
### JQMIGRATE: jQuery 3.0.0+ REQUIRED
**Cause:** The page does not have a version of jQuery installed, or is using a version of jQuery older than 3.0.0. The jQuery Migrate plugin is not intended to be used for those cases. Any messages that follow this one may not be accurate, or the page may not run properly at all.
This file has been truncated. show original
Thanks for the information.
My page is a database-driven page, so I’ve stripped out all the php and added static content.
Here is my JSFiddle - http://jsfiddle.net/webmachine/dcvwqwmy/7/
It seemed to work fine. So I took the same code and uploaded it to an old site I have that I use for testing, etc.
Here it did not work - http://jaybeeweb.com/ToDoList
I’m not sure why that’s not working, but this worked for me through the console:
$('#todo-list').on('click', 'h4', function(){
$(this).next().slideToggle()
})
Thank you. Both of your suggestions were great. I figured out why it worked in JS Fiddle and not in my page (I have never used JSFiddle before) - I forgot to enclose it in the JQuery document.ready function even though I had that in my original code. … kicking myself for missing that.
I guess JSFiddle doesn’t need that in the JS section
It’s not that - jsfiddle has scripting options that determine whether the script runs onload/ondomready/nowraphead/nowrapbody
Set your jsfiddle script to be nowraphead and you will see that it doesn’t work either, until the document.ready wrapper is in place.
Normally you should place your scripts at the end of the body, which with jsfiddle would be nowrapbody, which helps to prevent such issues.
It’s better to place the script at the bottom of the page just before the closing </body> tag. That way the DOM will be ready by default. Glad you got it working, though.
system
Closed
August 2, 2015, 1:28pm
11
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.