Avatar of csehz
csehz
Flag for Hungary asked on

JQuery - Toggle

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

Web Languages and StandardsJavaScript

Avatar of undefined
Last Comment
csehz

8/22/2022 - Mon
chaitu chaitu

$(document).ready(function() {
			$("h1").click(function() {
				$('h2').eq(1).slideToggle();
			});
		});    

Open in new window

csehz

ASKER
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"
Proculopsis

...or

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

...in your click handler
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
chaitu chaitu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
csehz

ASKER
Yes exactly thanks very much