Solved

JQuery - Toggle

Posted on 2012-03-13
5
488 Views
Last Modified: 2012-03-13
Dear Experts,

In the attached small code I am just trying the JQuery bases, could you please advise how a condition could be put to it?

I would like that the <h2> element would be toggled only, if its node value is "This is also a h2 element", so only the second one.

thanks,

<html>                                                                 
<head>                                                                 
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>       
	<script type="text/javascript">                                       
		$(document).ready(function() {
			$("h1").click(function() {
				$('h2').slideToggle();
			});
		});                                 
	</script>                                                             
</head>                                                               
<body>                                                                 
<h1>Click to here to hide the h2 element</h1>
<h2>This is a h2 element</h2>
<h2>This is also a h2 element</h2>                                  
</body>                                                               
</html>

Open in new window

0
Comment
Question by:csehz
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37714038
$(document).ready(function() {
			$("h1").click(function() {
				$('h2').eq(1).slideToggle();
			});
		});    

Open in new window

0
 
LVL 1

Author Comment

by:csehz
ID: 37714081
Thanks the solution, is it possible to make it also with using IF?

Basically I would like to see an example how a condition could be put. So around if "IF node value is something, then do toggle"
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37714084
...or

$(this).next().slideToggle();

...in your click handler
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 37714251
you mean this;

	$(document).ready(function() {
			$("h1").click(function() {
			alert($('h2').eq(1).text())
			   if($('h2').eq(1).text() == 'This is also a h2 element')
				$('h2').eq(1).slideToggle();
			});
		});

Open in new window

0
 
LVL 1

Author Closing Comment

by:csehz
ID: 37714414
Yes exactly thanks very much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now