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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    javascript getelementbyID within a loop problem

    I have the following code...the getelementbyId works fine it changes the value in the textfield aswell....but only in the first retrieved record...It is not working in any of the other records that are showed.

    Code is here:

    <?php

    include("../connection/conn.php");



    ?>
    <script type="text/javascript">
    function notEmpty(){
    var myTextField = document.getElementById('myText');
    document.getElementById('myText').innerHTML = myTextField;

    if(myTextField.value != "")
    alert("You entered: " + myTextField.value)
    else
    alert("Would you please enter some text?")
    }
    </script>

    </head>

    <body>
    <?php

    $sql=mysql_query("select * from menuscript");

    while($row=mysql_fetch_array($sql))
    {

    ?>

    <div class="mine">
    <?php echo $row['author'];?>


    <form>
    <input type='text' id='myText' />
    <input type='button' onclick="notEmpty();" value='Form Checker' /> </form>

    </div>

    <?php
    }?>

  • #2
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    A picture to show that only first textfield works and not the others


  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,632
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Your PHP loop is creating multiple

    <input type='text' id='myText' />

    tags and so is generating invalid HTML. Each id within a web page MUST have a different value. You can only have one id='myText' per page.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your reply....

    how can i make it to work as the values come from database and i cant assign different names. Do i need to use a for loop for it ?

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,632
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Set up a loop counter on the loop reading them in, increment the counter each time around the loop and add the current count to the end of the id so that each will be slightly different.

    You'll then need to pass the value of the id into the JavaScript function so it knows which id to update.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Can u give an example please......it kinda went like over my head

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,632
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    <?php
    
     include("../connection/conn.php");
    
    
    
     ?>
     <script type="text/javascript">
     function notEmpty(id){
     var myTextField = document.getElementById(id);
     document.getElementById(id).innerHTML = myTextField;
    
     if(myTextField.value != "")
     alert("You entered: " + myTextField.value)
     else
     alert("Would you please enter some text?") 
     }
     </script>
    
     </head>
    
     <body>
     <?php
     $count = 1;
     $sql=mysql_query("select * from menuscript");
    
     while($row=mysql_fetch_array($sql))
     {
    
     ?>
    
     <div class="mine">
     <?php echo $row['author'];?>
    
    
     <form>
     <input type='text' id='myText<?php echo $count ?>' />
     <input type='button' onclick="notEmpty('mytext<?php echo $count ?>' );" value='Form Checker' /> </form>
    
     </div>
    
     <?php
    $count++;
     }?>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    addyking (03-09-2012)

  • #8
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks mann! that was really great!


  •  

    Posting Permissions

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