Guardian-Mage Posted June 21, 2007 Share Posted June 21, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
FETNU Posted June 26, 2007 Share Posted June 26, 2007 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? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 27, 2007 Share Posted June 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.