Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JQuery - Toggle

Posted on 2012-03-13
5
Medium Priority
?
512 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

885 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