Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Setting div's height/width via javascript
Old 01-06-2009, 05:21 PM Setting div's height/width via javascript
Novice Talker

Posts: 5
Trades: 0
<html>
<title>
malakas
</title>
<body style="width:800px;height:600px;border:1px black solid;">

<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument(""," ",null);
}
else
{
alert('Your browser cannot handle this script');
}

if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("note4.xml");


var x=xmlDoc.getElementsByTagName("item");

for (i=0;i<13;i++)
{

title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;

description = x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;

score = x[i].getElementsByTagName("score")[0].childNodes[0].nodeValue;


document.write('<div id="mydiv" style="background:red">');
document.write(transform(score,"mydiv"));
document.write("<h3>"+title+"</h3>");
document.write("<br/>");
document.write(description);
document.write("<br/>");
document.write("Score : ");
document.write(score);
document.write("<br/>");
document.write("</div>");


}

}


function transform(scr,divname)
{
var div = document.getElementById(divname);



if(scr>0.7) {

div.style.height = 0.3*document.body.offsetHeight +'px';
div.style.width = 0.2*document.body.offsetWidth + 'px';
}



}





</script>

</body>
</html>



My problem is that my code changes the height/width of the first element only(with score>0.7) all the other elements-divs have the same height/width although some of them have score>0.7.
Why is that?
helppppppp pleaseeeeee!!!!!!!

Last edited by spirtokouto; 01-06-2009 at 05:24 PM..
spirtokouto is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-06-2009, 08:14 PM Re: Setting div's height/width via javascript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
IDs have to be unique.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-06-2009, 08:14 PM Re: Setting div's height/width via javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Because, without looking in depth, you are not using the element name but it's id.
An id value should be unique, if not, the browser returns the first one he find starting from top.
You must give different id to each divs you create, as for the parameter you pass to the transform() call.

Or really use the name attribute, and use a call to getElementsByName('theName) to get back not 1 DOM element, but an array of DOM elements.
https://developer.mozilla.org/En/DOM...mentsByTagName

Edit: d.a.m.n, Chris beat me to it, once again...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-06-2009, 08:55 PM Re: Setting div's height/width via javascript
Novice Talker

Posts: 5
Trades: 0
firstly thanks for your help so far!!!!!!
You mean i should have 13 different ids for my divs?
Is there an other way to do that?
Another mistake i noticed is that altough the first element has score=0.6 its height/width is modified!!!

Last edited by spirtokouto; 01-06-2009 at 09:31 PM..
spirtokouto is offline
Reply With Quote
View Public Profile
 
Old 01-07-2009, 03:35 AM Re: Setting div's height/width via javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
You mean i should have 13 different ids for my divs?
yes, or you should drop the id, and use the name and adapt your transform() function to get a list of the element with the same name and process them in a for loop.
Using DOM functions, your code would be written like:
Code:
//generate the divs
for(cptDiv=0;cptDiv<10;cptDiv++){
  newDiv=document.createElement('div');
  newDiv.name='myDiv'; //note it's .name, not .id that it's used here
  document.getElementsByTagName('body')[0].appendChild(newDiv);
}

function process(){
  var aryDivs=document.getElementsByName('myDiv'); //returns an array of 10 div's
  for(cptDiv=0;cptDiv<aryDivs.length;cptDiv++){
    elm=aryDivs[cptDiv]
    //Process your div here, working on "elm"
    ...
  }
}
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 01-07-2009 at 03:37 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Setting div's height/width via javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27859 seconds with 12 queries