Solved

How do I select the h3 tag inside a div? (prototype.js)

Posted on 2011-02-14
5
649 Views
Last Modified: 2012-05-11
<div id="indivpost1">
	<img src="th-1.jpg" />
	<h3></h3>
</div>

Open in new window


Shouldn't this work?
$$('#indivpost1 h3')

Open in new window


Please tell me what I'm doing wrong.
0
Comment
Question by:ram0nm
5 Comments
 
LVL 9

Expert Comment

by:CCSOFlag
ID: 34890970
your tag says javascript, but it looks like you are looking for JQuery maybe?

I don't use JQuery, but in Javascript it is:

var myDiv = document.getElementById('indivpost1');
var myTag = myDiv.getElementsByTagName('h3');


This will get you all h3 tags within that element.
0
 

Author Comment

by:ram0nm
ID: 34890991
Thanks for your help but I'm using Prototype not jQuery.
0
 
LVL 3

Expert Comment

by:JuanchoVzla
ID: 34891216
>>$$('#indivpost1 h3')
This should return an array with one element (with your example)
0
 
LVL 3

Expert Comment

by:JuanchoVzla
ID: 34891236
try breaking it down
$$('#indivpost1') .find('h3');
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34903878
To get the first one : $$("#indivpost1 h3").first();

Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script language="javascript">
	Event.observe(window, 'load', function() {
		alert( $$("#indivpost1 h3").first().innerHTML );
	});
</script>
</head>
<body>
<div id="indivpost1">
	<img src="http://www.codersadvocate.com/images/1245141859.gif" />
	<h3>First</h3>
	<h3>Second</h3>
</div>
</body>
</html>

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

762 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

20 Experts available now in Live!

Get 1:1 Help Now