views:

71

answers:

6

Hi everyone,

<div class="aList">
<div class="theTitle"></div>
<div class="theContent"></div>
</div>

I'm building a multi-feed RSS reader for school.

aList is the div that encompasses each individual feed (the amount of feeds will fluctuate).

theTitle is the div that will be filled with the attribute of the current feed. Additionally, if clicked, it will load a list of attributes from the current feed into theContent.

I'm wondering how I can dynamically load the attributes into theContent when theTitle is clicked, since theContent and theTitle are going to be non-unique divs (I can't give them IDs).

Thanks for your help in advance,

-Andrew

+1  A: 

document.getElementsByClassName('aList').getElementsByTagName('div')

ifwdev
honestly I haven't written code like this without using a library like MooTools in ages. I think mozilla natively supports getElementsByClassName but not IE. look into a custom implementation on google.
ifwdev
The answer here is about "getElementsByTagName", not class name. IE supports that.
Pointy
getElementsByClassName is there too...?
rjh
http://ejohn.org/blog/getelementsbyclassname-speed-comparison/There is a js implementation of getElementsByClassName there that works in IE.
ifwdev
+1  A: 

You should look into jQuery selectors for that and other DOM Manipulation. Something like

$("div.theContent").attr("name", "value");
HeavyWave
+1  A: 

by using jquery, you may use code like the following:

 $(".theTitle").bind("click", function(){
    $el = $(this);
           $el.parent().$(".theContent").load('ajax/content.php?news=' . $el.text());
 });

this will make all your links clickable, an on click, update their corresponding content divs with the value of ajax/content.php?news=theTitle-value

henchman
A: 

You have to be able to determine which element you want to update if you don't want to update more than one. If the elements are grouped inside something else that does have an "id" value, you can take advantage of that.

Pointy
+1  A: 
<div class="aList">
<div class="theTitle" onclick="fillContentBox(this)"></div>
<div class="theContent"></div>
</div>

And in your script ...

function fillContentBox(div) {
  var theContentDiv = div.parentNode.getElementsByTagName("div")[1];
  // statements that do things with theContentDiv
}
Robusto
+1  A: 

Use a nice Javascript library such as Prototype or jQuery. Seems petty now, but these frameworks save you tons of time in the long run.

In both frameworks, you can select that div with:

$('div.theTitle')

With jQuery, you can do:

$('div.theTitle').click( function() {
    var title = $(this).text();
    var contentDiv = $(this).siblings('div.theContent');
    // Do something with contentDiv and the title
} );

This will make every theTitle div have an onClick event that does something with its associated theContent div.

rjh
Just an FYI: You can shorten this to: `$(this).siblings('.theContent');` or shorter: `$(this).next('.theContent');`
Nick Craver
Updated post, thanks :)
rjh