PDA

View Full Version : Pass the Input text value in a TD cell using DOM


swetha123
01-29-2009, 10:44 AM
hello ,

I need to alert the value entered in the text box,which is in TD cell, using DOM

can any one tell how please
here is the code with out a text box which is working



<html>
<head>
<script type="text/javascript">
function cell()
{
var x=document.getElementById('myTable').rows[0].cells;
alert(x[0].innerHTML);
}
</script>
</head>
<body>

<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br />
<input type="button" onclick="cell()" value="Alert first cell">

</body>
</html>

here is what i need


<html>
<head>
<script type="text/javascript">
function cell()
{
var x=document.getElementById('myTable').rows[0].cells;
alert(x[1].firstChild.Value);
}
</script>
</head>
<body>

<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td><input type="text" id="txt"></td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br />
<input type="button" onclick="cell()" value="Alert first cell">

</body>
</html>
but i can't see the output if anything wromg please tell me

thanks
Swetha

swetha123
01-29-2009, 11:31 AM
hello

I need to alert the text what i entered in the text box and need to alert the price value which is to the side of the $ symbol

If remove the $ symbol and alert the price then i can see the Price
but i need the $ symbol there but in alert there is no need to pass the $ symbol only pass the price i.e 1000

here is my code


<html>
<head>
<script type="text/javascript">
function cell(i)
{
//var x=document.getElementById('myTable').rows[0].cells;
//alert(x[0].innerHTML);
var x=document.getElementById('myTable')
var y = x.rows[i].cells[1].firstChild.value;
var z = x.rows[i].cells[0].firstChild[getElementById(txt1).value];

alert(y);
alert(z);
}
</script>
</head>
<body>
<?php
$price=1000;
echo'<table id="myTable" border="1">';
for ($i=0; $i<3; $i++)
{
echo'<tr>
<td>$<input type="text" id="txt1" value="'.$price.'" disabled/></td>
<td> <input type="text" id="txt"/></td>
<td> <input type="button" onClick="cell('.$i.')" value="Alert"></td>
</tr>';
}
echo'</table>';
?>
</body>
</html>

davidj
01-29-2009, 01:26 PM
can you not use a div as a place holder where you want the data to appear



<!-- used in cell -->

<div id="data"></div>





document.getElementById("data").innerHTML = document.getElementById("textfield").value

domedia
01-29-2009, 01:30 PM
You can use whatever, as long as it has an ID attribute, even a table cell.

davidj
01-29-2009, 01:44 PM
yes true

but by inserting an placeholder which can be positioned within the TD i would asume you would have better positioning control

domedia
01-29-2009, 01:55 PM
Table cells are usually pretty easy. You want the data in the grid to be aligned to left/right, style the text maybe, add some padding/margin to the box etc. Unless you're using tables for layout, in which case you're screwed anyways, you can simply use classes, right use of th/td etc to style regular data tables. I can see where you're coming from, but it's seems redundant.