473,326 Members | 1,972 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,326 software developers and data experts.

Find all IDs from the className

Because of the nature of what im trying to do, the ids of the divs im creating are all from a foreach loop array but the div is classed so when i click an onsubmit i need to find all the IDs for a particular className and put them all into an array.

How can i do this.

[php]
foreach($sections as $section)
{
$parts = explode('{', $section);
$selector = trim($parts[0]);
$elements = explode(';', $parts[1]);
// below is the div im on about
echo "\n\n<br><div class=\"selector\" id=\"$selector\">".$selector."</div><br>\n\n";
[/php]

Above is the what i have done to create the divs.
Feb 27 '07 #1
8 3807
AricC
1,892 Expert 1GB
Because of the nature of what im trying to do, the ids of the divs im creating are all from a foreach loop array but the div is classed so when i click an onsubmit i need to find all the IDs for a particular className and put them all into an array.

How can i do this.

[php]
foreach($sections as $section)
{
$parts = explode('{', $section);
$selector = trim($parts[0]);
$elements = explode(';', $parts[1]);
// below is the div im on about
echo "\n\n<br><div class=\"selector\" id=\"$selector\">".$selector."</div><br>\n\n";
[/php]

Above is the what i have done to create the divs.
Is this a PHP question or Javascript?
Feb 27 '07 #2
This is a javascript question, im just demonstrating how i have created the divs and how each id from the div is differernt, but the className is always the same.

I want to find all the ID names from a parictular className in javascript. For example:

<div class="test" id="hello"></div>
<div class="test" id="anotherID"></div>
<div class="test" id="getTheIdea"></div>
<div class="test" id="IDisDifferentfromClassName"></div>

So i want to do a:

var getID=document.getElementByClassName('test');

var something="find all the ids from the className 'test';

var arrayID=" get all the IDS and put them into an array"


DO you see what i want to do?
Feb 27 '07 #3
acoder
16,027 Expert Mod 8TB
There are many implementations of getElementsByClassName, e.g.
Dustin Diaz
Robert Nyman
Then get the ids of these objects by looping through them.
Feb 27 '07 #4
Is there anything wrong with this:

Expand|Select|Wrap|Line Numbers
  1. function getElements()
  2.   {
  3.   var x=document.getElementsByClassName("selector");
  4.   alert(x.length);
  5.   }
  6.  
it works if i use getElementsByName? Also does mozilla support getElementsByClassName?
Feb 27 '07 #5
Expand|Select|Wrap|Line Numbers
  1. divs = document.getElementsByTagName("div");
  2. idArray = new Array();
  3.  
  4.     for (x=0; x<divs.length; x++) {
  5.         if (divs[x].className=="classe") {
  6.         idArray[x] = divs[x].id
  7.         }
  8.     }
  9.  
classe is my test class
divs my variable to hold the tags divs
idarray my test array
Feb 27 '07 #6
acoder
16,027 Expert Mod 8TB
Is there anything wrong with this:

Expand|Select|Wrap|Line Numbers
  1. function getElements()
  2.   {
  3.   var x=document.getElementsByClassName("selector");
  4.   alert(x.length);
  5.   }
  6.  
it works if i use getElementsByName? Also does mozilla support getElementsByClassName?
getElementsByClassName is a function, not a method of document. I don't think it is supported. It may be supported in later releases of browsers. That's why you have to write your own.
Feb 27 '07 #7
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. divs = document.getElementsByTagName("div");
  2. idArray = new Array();
  3.  
  4.     for (x=0; x<divs.length; x++) {
  5.         if (divs[x].className=="classe") {
  6.         idArray[x] = divs[x].id
  7.         }
  8.     }
  9.  
classe is my test class
divs my variable to hold the tags divs
idarray my test array
Good solution and will probably be sufficient in this situation, but it doesn't take into account multiple classes, e.g.
Expand|Select|Wrap|Line Numbers
  1. <div class="class1 class2" id="...">
Feb 27 '07 #8
There are many implementations of getElementsByClassName, e.g.
Dustin Diaz
Robert Nyman
Then get the ids of these objects by looping through them.
You are right! And here's another one: xGetElementsByClassName(sClsName, oParentEle, sTagName, fnCallback).

It can be used in two different ways:

(1) Iterate through the returned array:
Expand|Select|Wrap|Line Numbers
  1. var i, a = xGetElementsByClassName(sClsName, oParentEle, sTagName); 
  2. for (i = 0; i < a.length; ++i) {
  3.   // a[i] is an element with sClsName as one of its class names
  4. }
  5.  
(2) Provide a callback function:
Expand|Select|Wrap|Line Numbers
  1. xGetElementsByClassName(sClsName, oParentEle, sTagName,
  2.   function(ele) {
  3.     // ele is an element with sClsName as one of its class names
  4.   }
  5. ); 
  6.  
:)
Feb 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I have been trying to dynamically call a static member function, as follows: $className = 'MyClass'; $methodName = 'MyMethod' $result = $className::$methodName(); However, I get a...
1
by: Peter King | last post by:
if you assign multiple classes in order to keep your classes generic e.g ..classA { position.absolute; left:5 top:5 height:200 width:800 } ..classB { background-color: red} ..classC {...
4
by: Ben R. | last post by:
Between ClassName and Inherits, which attribute is set to specify the class that a page uses? I would think that would be inherits. Further, the description for ClassName is: Specifies the class...
2
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel =...
1
by: enahar | last post by:
Hello, i want to find out how many instances of a form (say childForm1) is running using FindWindowEx but value return is always zero by this method. What wrong I am doing it..Can anybody...
2
by: bmgz | last post by:
I have written a script that highlights a table row when the appropriate checkbox is checked. Using element.style is a bit messy and doesn't really fulfil my needs.. I want to just be able to...
2
by: arne | last post by:
Hi what would be the easiest way to find all tag's with a certain name (or class) below (within) a certain tag. Pref jscript DOM thx Arne
0
by: Jacob Avlund | last post by:
Hello all, I wish to load two user controls (cart.ascx and products.ascx) programatically. What I'm doing right now: In my aspx file, two references are added: <%@ Reference...
2
by: benjamin | last post by:
I'm new at JS and DOM, so please bear with me... Here's what I'm trying to do: My code is such as this: *** <div class="post" id="123"> lorem ipsum <div class="tag1">hello</div> </div>
5
by: laredotornado | last post by:
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"? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.