473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Prototype.js: How to find element(s) by class?

Hi,

Please let me know if there is a better place to which to post this
message. I'm trying to use prototype.js and wondering how I use it to
find elements on page with class = "myClass"?

Thanks, - Dave
Aug 12 '08 #1
5 39624
On Aug 12, 1:12*pm, laredotornado <laredotorn...@zipmail.comwrote:
Hi,

Please let me know if there is a better place to which to post this
message. *I'm trying to use prototype.js and wondering how I use it to
find elements on page with class = "myClass"?

Thanks, - Dave
Just search the groups for prototype. There is a group for it.

And the getElementsByClass was depricated in prototype 1.6 or what
ever the latest is. so if you want to use it you might have to find
an older version.
Aug 12 '08 #2
On Aug 12, 1:12*pm, laredotornado <laredotorn...@zipmail.comwrote:
Hi,

Please let me know if there is a better place to which to post this
message. *I'm trying to use prototype.js and wondering how I use it to
find elements on page with class = "myClass"?

Thanks, - Dave
you also could do this pretty easily on your own with out prototype
especially if the class is tag specific. Get a collection of tags
that the class is supposed to apply to say <td>. Then loop through
then checking there class value to see if it equals your 'myClass'.

Then do something with it when you find one or store them in an array
to do something with them later.
Aug 12 '08 #3
On Aug 12, 10:22*am, GinnTech <Tim.Dale.G...@gmail.comwrote:
you also could do this pretty easily on your own with out prototype
especially if the class is tag specific. *Get a collection of tags
that the class is supposed to apply to say <td>. *Then loop through
then checking there class value to see if it equals your 'myClass'.
You actually don't want to check if it's just equal. Since you could
have an element like <p class="myClass classtwo"you would want to
check the indexOf('myClass') != -1 in that string. The problem with
this is that you will get false positives for things like <p
class="myClass2">. There are a couple options here, you could use
regular expressions (haven't really tested this but something like: /
([\w-]+\s+)*myClass(\s+[\w-]+)*/)or you could split on white space and
then iterate over the array which came back. I suspect that regular
expressions are going to be faster.
But if you're concerned with performance then checking to see if the
browser has its own getElementsByClassName method or a querySelector
that it makes public to the JS then you should use those and only
create one for yourself if those are not available. Also smart to
search/keep up on which engines support or have plans to support which
methods.

--
Dan

Aug 13 '08 #4
On Aug 14, 7:48 am, Dan Evans <TheDanEv...@gmail.comwrote:
On Aug 12, 10:22 am, GinnTech <Tim.Dale.G...@gmail.comwrote:you also could do this pretty easily on your own with out prototype
especially if the class is tag specific. Get a collection of tags
that the class is supposed to apply to say <td>. Then loop through
then checking there class value to see if it equals your 'myClass'.

You actually don't want to check if it's just equal. Since you could
have an element like <p class="myClass classtwo"you would want to
check the indexOf('myClass') != -1 in that string. The problem with
this is that you will get false positives for things like <p
class="myClass2">. There are a couple options here, you could use
regular expressions (haven't really tested this but something like: /
([\w-]+\s+)*myClass(\s+[\w-]+)*/)
A pretty common regular expression is:

/(?:^|\\s+)className(?:\\s+|$)/
and used like:

function hasClass(element, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(element.className);
}
There is discussion on this in the archives.
--
Rob
Aug 14 '08 #5
@Dave

For Prototype you should use $$() or Element.select():
http://www.prototypejs.org/api/utility/dollar-dollar
http://www.prototypejs.org/api/element/select

Examples:
$$('.class-a,.class-b');
$$('div[class~="class-a"]')
$$('td:not(.class-b)');
$(element).select('.class-a');

Many other element methods support css selectors like
Element.up, Element.down, Element.next, Element.previous,
Element.adjacent, Element.match

For Prototype questions and discussions:
http://www.prototypejs.org/discuss

- JDD
Aug 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sorincom | last post by:
Hello, I have this class: public class TestClass { private string testData = ""; public string TestData { get { return this.testData; }
2
by: Michal Januszczyk | last post by:
I have a very simple XML document. <SmtpGatewayRules xmlns="http://www.foo.com/schema.xsd"> <rules> <rule> </rule> </rules> </SmtpGatewayRules> When no xmlns is specified I can find rule...
1
by: Victor Hadianto | last post by:
Hi, I have a simple XSD for example like this: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
9
by: Doru Roman | last post by:
Hi, I try to use Process to open a Word document from my application. The problem is I don't know what namespace to use in the Using statement so that : Process myProcess = new Process(); ...
1
by: SoxFan44 | last post by:
Hi, I have a DataGridView on my Form1 and a public function that can add a row to it. I want to be able to access that member function from another class. I'm confused as to how to do this. Can...
4
by: Joachim | last post by:
How can you find out if a class is inherited from a certain other class? Is there another way than using .BaseType several times?
0
by: electrical | last post by:
/** Database implements a database of records */ public class Database { private Record base; // the collection of records private int NOT_FOUND = -1; // int used to denote when a...
3
pod
by: pod | last post by:
I work in MS Visual Studio 2008, I have a VB.NET Solution which has three Projects, BusinessObjectLibrary (object properties), DataAccessLibrary (object functions such as selectAll, exportToExcel)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.