...

View Full Version : need help using variable to get elementID



Alith7
07-29-2009, 03:44 PM
Here is the code I have:

var xmlhttpInk;

function addInkOpt(iside,ivalue)
{
xmlhttpInk=GetXmlHttpObject();
if (xmlhttpInk==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="/scripts/addink.php";
url=url+"?q="+iside+"&k="+ivalue;
url=url+"&sid="+Math.random();
xmlhttpInk.onreadystatechange=InkChange(iside,ivalue);
xmlhttpInk.open("GET",url,true);
xmlhttpInk.send(null);
}

function InkChange(s,v)
{
var inkdivID="ink"+s+v;
if (xmlhttpInk.readyState==4)
{
document.getElementById(inkdivID).innerHTML=xmlhttpInk.responseText;
}
}


<div id="ink1div">
<h3>Inks Side 1</h3>
<div id="ink11">
<p>Ink:
<select name="InkID11" class="dropMenu" id="InkID11" onchange="addInkOpt(1,2)">
<option value="NULL"></option>
<?php do { ?>
<option value="<?php echo $row_inkList['InkID']?>"><?php echo $row_inkList['InkDesc']?></option>
<?php } while ($row_inkList = mysql_fetch_assoc($inkList));
$rows = mysql_num_rows($inkList);
if($rows > 0) {
mysql_data_seek($inkList, 0);
$row_inkList = mysql_fetch_assoc($inkList);
} ?>
</select>Desc:
<input name="Ink1Desc1" type="text" class="inkdesc" id="Ink1Desc1" maxlength="25" />
</p>
</div>
<div id="ink12"></div>
<div id="ink13"></div>
<div id="ink14"></div>
<div id="ink15"></div>
<div id="ink16"></div>
<div id="ink17"></div>
</div>
<div id="ink2div">
<h3>Inks Side 2</h3>
<div id="ink21">
<p>Ink:
<select name="InkID21" class="dropMenu" id="InkID21" onchange="addInkOpt(2,2)>
<option value="NULL"></option>
<?php do { ?>
<option value="<?php echo $row_inkList['InkID']?>"><?php echo $row_inkList['InkDesc']?></option>
<?php } while ($row_inkList = mysql_fetch_assoc($inkList));
$rows = mysql_num_rows($inkList);
if($rows > 0) {
mysql_data_seek($inkList, 0);
$row_inkList = mysql_fetch_assoc($inkList);
} ?>
</select>Desc:
<input name="Ink2Desc1" type="text" class="inkdesc" id="Ink2Desc1" maxlength="25" />
</p>
</div>
<div id="ink22"></div>
<div id="ink23"></div>
<div id="ink24"></div>
<div id="ink25"></div>
<div id="ink26"></div>
<div id="ink27"></div>
</div>

could someone please help me figure out what I'm doing wrong?

wldrumstcs
07-29-2009, 03:47 PM
Nevermind........

Alith7
07-29-2009, 05:24 PM
figured it out.
I had to move the var outside both functions, and add an empty string in the getElementById()

Here's the final code if anyone is interested.

var xmlhttpInk;
var inkdivID;

function addInkOpt(iside,ivalue)
{
xmlhttpInk=GetXmlHttpObject();
if (xmlhttpInk==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="/scripts/addink.php";
url=url+"?q="+iside+"&k="+ivalue;
url=url+"&sid="+Math.random();
inkdivID="ink"+iside+ivalue;
xmlhttpInk.onreadystatechange=InkChange;
xmlhttpInk.open("GET",url,true);
xmlhttpInk.send(null);
}

function InkChange()
{
if (xmlhttpInk.readyState==4)
{
document.getElementById(inkdivID+"").innerHTML=xmlhttpInk.responseText;
}
}

ckeyrouz
07-29-2009, 05:28 PM
We did not know what was the problem at the first.

Alith7
07-29-2009, 05:46 PM
sorry, I should have said specifically, it's not working. originally, when I changed the dropdown, it did nothing.

Alith7
07-29-2009, 07:57 PM
ok, new question.
is there a way to do the same thing without having all the DIV tags in already? somehow get the javascript to generate the DIV tag and the info, or just add what's in the <p> tags to the same div?

ckeyrouz
07-29-2009, 08:16 PM
try this:

var newDiv = document.createElement("div");
newDiv.innerHTML = "<p>all your html code here</p>"; // this could be as well xmlhttpInk.responseText;

var objToInserBefore = document.getElementById("someId");
objToInserBefore.parentNode.insertBefore(newDiv,objToInserBefore);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum