?
Solved

Javascript / Prototype library select by CSS name

Posted on 2007-08-12
2
Medium Priority
?
709 Views
Last Modified: 2008-02-01
Hi,
I've been using Javascript for some time but the prototype library is new to me;

I'm trying to use select element by CSS name functions of prototype, without success.

Bellow is a sample test the standard dom method and the prototype $ method work.
But getElementsByClassName and $$ don't work.

Can any body tell me why test 3 & 4 don't work in the following example.
-- Prototype version: 1.5.1.1
-- Firefox version 2.0.0.6

<html>
<head>
<script language="javascript" src="prototype.js"></script>
<script language="javascript">
function test(t){
      if( t == 1 ) oReadonly = document.getElementById('age'); // This works
      if( t == 2 )  oReadonly = $('age'); // This works
      if( t == 3 )  oReadonly = $$('.readonly'); // This dosn't work
      if( t == 4 ) oReadonly = document.getElementsByClassName('readonly'); // This dosn't work
      alert( oReadonly+" name: "+oReadonly.name );
      Field.disable(oReadonly);
      oReadonly.disable();
}
</script>
</head>
<body>
<form method="post" action="testme.php">
<input name="name" type="text" value="Micky Mouse"><br>
<input id="age" class="readonly" name="Age" type="text" value="82"><br>
<input type="button" value="Test 1" onClick="test(1);">
<input type="button" value="Test 2" onClick="test(2);">
<input type="button" value="Test 3" onClick="test(3);">
<input type="button" value="Test 4" onClick="test(4);">
</form>
</body>
</html>
0
Comment
Question by:Matthew_Way
1 Comment
 
LVL 11

Accepted Solution

by:
BraveBrain earned 2000 total points
ID: 19679011
     if( t == 3 )  oReadonly = $$('.readonly'); // This dosn't work
      if( t == 4 ) oReadonly = document.getElementsByClassName('readonly'); // This dosn't work

Both of the above return an array of matching elements.
Change the two lines as shown below and they will return the first element in the array (in your case the age input, as that is the only one with the readonly class.
      if( t == 3 )  oReadonly = $$('.readonly')[0];
      if( t == 4 ) oReadonly = document.getElementsByClassName('readonly')[0];

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

807 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