Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help onclick different values

    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
    PHP Code:
    <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==&& xmlhttp.status==200)
        {
        
    document.getElementById("output").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","session_sch.php?q="+str,true);
    xmlhttp.send();


    PHP Code:
       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>'

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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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.

  • #3
    Regular Coder
    Join Date
    May 2010
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


    Code:
    <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>

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Much easier:
    Code:
    <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:
    Code:
    function getelem(obj)
    
    {
     var TestVar = obj.value;
     document.getElementById('output').value=TestVar ;
    }

  • #5
    Regular Coder
    Join Date
    May 2010
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah that works thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •