Click to See Complete Forum and Search --> : getElementById(variable)...


IxxI
05-14-2003, 09:24 AM
I have lots of div boxes which I want to do the same thing, but as I move my mouse over different things. Is there a way of passing the name of a div to a function so that I can have one function instead of loads. Example: say I have a function move() can I call it move(dvnm) then have document.getElementById(dvnm).style.left=whatever, and then in my mouseover statement have move(div1), where div1 is the name of my first div. I know I can't do exactly this because I tried, but can I do something like this?

IxxI

pyro
05-14-2003, 09:32 AM
You must have your syntax off, because this works for me...

<html>
<head>

<script language="javascript" type="text/javascript">
function move(what) {
document.getElementById(what).style.left = "100px";
}
</script>

</head>

<body>

Move <a href="#" onclick="move('mydiv01');">one</a>
<br/>
Move <a href="#" onclick="move('mydiv02');">two</a>

<div id="mydiv01" style="position:relative; left:0px;">Div One</div>
<div id="mydiv02" style="position:relative; left:0px;">Div Two</div>

</body>
</html>

IxxI
05-14-2003, 10:35 AM
Yeah, sorry just being stupid...

IxxI

zoom411030
05-01-2008, 12:31 AM
i have a function like this ..

function showall(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="worldnews.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged(str)
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged(divname)
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{// here divname not getting accepted :confused:
document.getElementById(divname).innerHTML=xmlHttp.responseText
}
else{
var newHTML = "<center><br><br><br><br><img src='waiticon.gif'></center>";
document.getElementById(divname).innerHTML=newHTML
}

can ne 1 help me to tress out my mistake..
help ll b appreciated
Shweta..

Kor
05-01-2008, 12:50 AM
i have a function like this ..


Please, don't invade other threads with your problem which has nothing to do with the topic. Open a new thread of your own and put your problem there.

Poster1
04-25-2009, 07:08 AM
Guy's,

I have the exact same problem and it's driving me crazy for months.
I cant use a variable within document.getElementById.
Down here pyro says that syntax needs to be off. I don't know what he means by that.
Here's the code I use:

function big(id)
{
document.getElementById("bigimg").src = id;
document.getElementById("bigimg").height = "363";
document.getElementById("bigimg").width = "340";
}

and this is calling the function:

<img src="M4110104.JPG" height="91" width="85" onclick="big(1)" id="1"/>

Fang
04-25-2009, 07:17 AM
ID must begin with a letter: http://www.w3.org/TR/html4/types.html#type-id

What are you trying to do?

Poster1
04-25-2009, 07:25 AM
Tryd it with a letter, but it still don't work.
I'm trying to make an image big by clicking on it.
So, I have two pictures on the page. When you click on the small one the big one will get its source. But when I parse the id to the function and put it in document.getElementById it returns me a undefind variable.
It has never worked befor.

Poster1
04-25-2009, 07:29 AM
Sorry, forgot a few lines.
function big(id)
{
var src=document.getElementById(id).src;
document.getElementById("bigimg").src = src;
}
<img src="M4110104.JPG" height="91" width="85" onclick="big(img1)" id="img1"/>

Fang
04-25-2009, 07:48 AM
function big(obj)
{
obj.height = "363";
obj.width = "340";
}

<img src="M4110104.JPG" height="91" width="85" onclick="big(this)">

Poster1
04-25-2009, 07:51 AM
No, that's not what I mean.
I'm using 2 images. One big and the other one small.
Look here: www.bkmbouwbedrijf.nl/wagenpark.html

Fang
04-25-2009, 08:05 AM
Rename the images:
'imageNameThumb' and 'imageName'
function big(obj)
{
document.images['vergroot'].src = obj.src.replace(/Thumb/g,'');
}


<img src="defaultImage.jpg" height="363" width="340" name="vergroot">

<img src="appleThumb.jpg" height="91" width="85" onclick="big(this)">

Poster1
04-25-2009, 08:25 AM
So, it's not possible to use a variable within document.getElementById()? The problem is that I'll have to use this function for a other page(projects) where the images are generated with php. Is thear an other way so I dont have to rename the images?

Poster1
04-25-2009, 08:34 AM
Ohh man. Thank you very very very much. I figured it out.
Don't even need to replace text.
Check it out.

function big(obj)
{
document.images['vergroot'].src = obj.src;
}


<img src="defaultImage.jpg" height="363" width="340" name="vergroot">

<img src="appleThumb.jpg" height="91" width="85" onclick="big(this)">