...

View Full Version : help onclick different values



Anishgiri
01-11-2011, 07:30 AM
What this code does is the table fetch two times. Column Sch_Code has two values which is 13222 and 23333, when I click the SCHCODE column first row value, it display the value which is 13222 on the input text box(that is what it supposed to do), the problem is when I click the second row value it still display 13222 instead of 23333.

ajax

<script type="text/javascript">
function getelem(forms)
{


var TestVar =document.getElementById('sch_code').value;
document.getElementById('sch_code2').value=TestVar ;
if (str=="default value")
{
return false;
}

if (str==null)
{
return false;
}

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("output").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","session_sch.php?q="+str,true);
xmlhttp.send();
}



echo '
<table >
<tr>
<th id="th_style">SCHCODE</th>
</tr>
';
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{

echo'
<tr>
<td id="td_style" ><input type="text" size="17" class="inputformat" id="sch_code" value="'.$row['sch_code'].'" onclick="getelem()"></td>
</tr>
';
}

echo ' </table>';




<input type="text" size="17" id="sch_code2">

devnull69
01-11-2011, 09:46 AM
I see you have the same id="sch_code" in each row. This can not work, as the id attribute has to be unique for all the elements of a page document. If you address getElementById('sch_code') you will only get the first one (in most browsers), but the result may be unpredictable.

Anishgiri
01-11-2011, 10:17 AM
I think it can be done if you use getelementsbyname, I tried but I can't get it done.

I created a simple code to make things simple, here when click one of the input box, its value should reflect on the input box with output as its id. I know it can be done easily if the name of input box is unique, but that is not what I want.



<html>
<head>

<script type="text/javascript">


function getelem()

{
var TestVar =document.getElementsByName('dummy_name')[0].value;
document.getElementById('output').value=TestVar ;
}

</script>
</head>

<body>
<form>
<input type="text" name="dummy_name" value="one" onclick="getelem()"><br>
<input type="text" name="dummy_name" value="two" onclick="getelem()"><br>
<input type="text" name="dummy_name" value="three" onclick="getelem()"><br>

<br><br>
<input type="text" id="output">
</form>
</body>
</html>

devnull69
01-11-2011, 10:27 AM
Much easier:


<form>
<input type="text" name="dummy_name" value="one" onclick="getelem(this)"><br>
<input type="text" name="dummy_name" value="two" onclick="getelem(this)"><br>
<input type="text" name="dummy_name" value="three" onclick="getelem(this)"><br>

<br><br>
<input type="text" id="output">
</form>


Javascript:


function getelem(obj)

{
var TestVar = obj.value;
document.getElementById('output').value=TestVar ;
}

Anishgiri
01-11-2011, 11:15 AM
Yeah that works thank you very much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum