Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery - Toggle

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

722 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