Solved

JQuery - Toggle

Posted on 2012-03-13
5
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
web page freezes after ajax post 7 102
Prevent certain words from being typed in a form 6 57
How to size popup in jquery mobile 4 63
Drag & Drop Error 5 33
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 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