Js function to change respecive cell value onchange selected value of dropdown value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tarunkhatri
    New Member
    • Sep 2009
    • 36

    Js function to change respecive cell value onchange selected value of dropdown value

    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.

    Code:
    <script type="text/javascript"> 
    function displaytaskid(ths) { 
        var id= ths.options[ths.selectedIndex].value;  
        alert (id);
        document.getElementById('task_id1').innerHTML=id;
    } 
    </script>
    Code:
    <?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>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    of course you have to give out unique ids to get it working.

    Comment

    • tarunkhatri
      New Member
      • Sep 2009
      • 36

      #3
      Yeahh Dormi I know tht. But I really dnt knw how to change my code to do that.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what about
        Code:
        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.

        Comment

        • tarunkhatri
          New Member
          • Sep 2009
          • 36

          #5
          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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            how much do you know about DOM?

            (you have 2 td per tr?)

            Comment

            • tarunkhatri
              New Member
              • Sep 2009
              • 36

              #7
              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.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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

                Comment

                • tarunkhatri
                  New Member
                  • Sep 2009
                  • 36

                  #9
                  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.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    I have an idea.
                    Code:
                    parentNode.firstChild().nodeValue = this.value;
                    case closed*.

                    you need to know DOM to understand the meaning of that. and event listeners.


                    * – for me

                    Comment

                    • tarunkhatri
                      New Member
                      • Sep 2009
                      • 36

                      #11
                      Thanks for your effort.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        and now for a working (FF) example
                        Code:
                        function setTD()
                        {
                        	this.parentNode.parentNode.firstElementChild.textContent = this.value;
                        }
                        // addEventForEach() is a custom function
                        document.getElementsByTagName("select").addEventForEach("change", setTD, false);

                        Comment

                        • tarunkhatri
                          New Member
                          • Sep 2009
                          • 36

                          #13
                          I have changed my code to below.. but still dosnt work.?? does any one has a solution
                          Code:
                          <script type="text/javascript"> 
                          function displaytaskid(ths,rowno) { 
                              var id= ths.options[ths.selectedIndex].value;  
                              alert (id);
                             document.getElementById('task_id_rowno').innerHTML= id;
                          }
                          Code:
                          <?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>

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            JS, unlike PHP, does not search for variable names inside strings.

                            Comment

                            • tarunkhatri
                              New Member
                              • Sep 2009
                              • 36

                              #15
                              humm ok... I changed it to
                              Code:
                               document.getElementById(rowno).innerHTML= id;
                              but still no result
                              Last edited by Dormilich; Sep 30 '09, 03:44 PM. Reason: Please use [code] tags when posting code

                              Comment

                              Working...