Solved

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

Posted on 2011-02-14
5
657 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 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 …
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)

828 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