Js function to change respecive cell value onchange selected value of dropdown value | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| |
Hi. I have a javascript function to change the value of the cell on chnage event of a dropdown list.My code is workin but the problem is that the value of the cell is chaning but not respective to the selected dropdown list( as there are 15 dropdown list and 15 cells), but only on the 1st cell. So need some identification for each cell. Can anyone help me just refine my code. - <script type="text/javascript">
-
function displaytaskid(ths) {
-
var id= ths.options[ths.selectedIndex].value;
-
alert (id);
-
document.getElementById('task_id1').innerHTML=id;
-
}
-
</script>
- <?php for($count; $count <15; $count++)
-
{?>
-
-
<tr>
-
<td id ="task_id1[]"></td>
-
<td name = "task_id[]"><select name="task_id" id="dropdown" onchange="displaytaskid(this)">
-
<option value=""></option>5
-
<?php
-
-
while($lp_row = pg_fetch_assoc($res2))
-
{
-
$val = $lp_row["task_id"];
-
$caption = $lp_row["task_description"]; ?>
-
<option value="<?php echo $val ?>"><?php echo $caption ?></option>
-
<?php } ?></select>
-
</td>
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
of course you have to give out unique ids to get it working.
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
Yeahh Dormi I know tht. But I really dnt knw how to change my code to do that.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
what about - echo "<td id=\"task_id_$count\">";
?
besides, you don’t even have to use ids, since the select and the td have the same parent.
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
THanks Dormi, sounds a good idea.. but then how to identify it by the js function(what change I need to do to my JS function) to identify each cell with the respective change in the dropdown list
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
how much do you know about DOM?
(you have 2 td per tr?)
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
yes there are 2 td per tr.. In one td there is the cell where i want my value to be changed and in 2nd is the actual dropdown list. which is trigerrin the js function. A little bit about DOM. Whenevery this JS bit comes in my code i get confused as I m really not good at it.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value Quote:
Originally Posted by tarunkhatri Whenevery this JS bit comes in my code i get confused as I m really not good at it. time to get better… if you want to advance in JS, you need to know DOM. because DOM is what makes modifying HTML documents easy.
first some stuff to read: DOM Intro (there are further articles listed) DOM Intro @ W3C
some technical stuff the DOM Specifications the DOM IDL | | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
Thanks vry much Dormi. This wud be really helpful. I know I really have to read this otherwise I ll keep on hanging around. But still rgt now if u have a solution for above do let me knw.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
I have an idea. - parentNode.firstChild().nodeValue = this.value;
case closed*.
you need to know DOM to understand the meaning of that. and event listeners.
* – for me
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
Thanks for your effort.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
and now for a working (FF) example - function setTD()
-
{
-
this.parentNode.parentNode.firstElementChild.textContent = this.value;
-
}
-
// addEventForEach() is a custom function
-
document.getElementsByTagName("select").addEventForEach("change", setTD, false);
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
I have changed my code to below.. but still dosnt work.?? does any one has a solution - <script type="text/javascript">
-
function displaytaskid(ths,rowno) {
-
var id= ths.options[ths.selectedIndex].value;
-
alert (id);
-
document.getElementById('task_id_rowno').innerHTML= id;
-
}
-
- <?php for($count; $count <15; $count++)
-
{?>
-
-
<tr>
-
<?php echo "<td id=\"task_id_$count\">"; ?>
-
<?php
-
$conn = connect();
-
$res2 = sql_select1();
-
?>
-
-
<td name = "task_id[]"><select name="task_id" id="dropdown" onchange="displaytaskid(this,\"$count\")">
-
<option value=""></option>5
-
<?php
-
-
while($lp_row = pg_fetch_assoc($res2))
-
{
-
$val = $lp_row["task_id"];
-
$caption = $lp_row["task_description"]; ?>
-
<option value="<?php echo $val ?>"><?php echo $caption ?></option>
-
<?php } ?></select>
-
</td>
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
JS, unlike PHP, does not search for variable names inside strings.
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
humm ok... I changed it to - document.getElementById(rowno).innerHTML= id;
but still no result
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,662
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
if rowno is not the ID then you have no target element.
note: can you see, how useful the DOM is?
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Js function to change respecive cell value onchange selected value of dropdown value
Thanks Dormi...I got a way around passinf count variable was doin all the prob..Anyways thanks very much for ur help
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|