Jump to content

Find all divs with news in it?


Guardian-Mage

Recommended Posts

For the website I am developing, I need to find all divs with "news" in there id and set their height. For example, I must execute setHeight on news1,news2,news3, and news4. Since the site is dynamic, I can't just specify the divs as there could be more or less. My determineNews() function will be used to find all divs with "news" in their id and execute setHeight() with the parameter of the div.

For the above example I would need to execute

setHeight('news1');

setHeight('news2');

setHeight('news3');

setHeight('news4');

 

I just need someone to help me with how to do this.

 

function determineNews()
{

}
function setHeight(obj_id)
{
//Find the div's height
var heightObjId= document.getElementById(obj_id); // Get div element
var heightObjHeight = heightObjId.offsetHeight; // div height
var targetDiv = document.getElementById(obj_id);

//Change that div's height to the propper height and set display to none
targetDiv.style.height = heightObjHeight + "px";
targetDiv.style.display = "none";
}

Link to comment
Share on other sites

Are all the div's on the same page?  If so, does your example reflect the naming convention you use?  If so... then I would run through a for loop on your entire page.  I did not test this code at all, but maybe use it more as pseudo code to help give you an idea...

 

function determineNews()
{
var Id = 1;
var newsDiv = 'news' + Id;

for (i=0; i<document.forms[0].length; i++)
  {
  if (document.getElementById(newsDiv).innerHTML != "")
    {
      setHeight(document.getElementById(newsDiv));
      Id = Id + 1;
    }
  }
}

 

Would something like that work for you?

Link to comment
Share on other sites

all the divs whose id has the word news?

 

var all_divs = document.getElementsByTagName('div');

var count = all_divs.length;

var news_divs = [];

 

for(var i = 0; i < count; i++){

if(all_divs.id.indexOf('news') > 0){

news_divs.push(all_divs.id);

}

}

 

news_divs should now be an array containing all of the id's of the divs that have an id with the word news in it

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.