www.webdeveloper.com
Results 1 to 9 of 9

Thread: Getting random number to show up in text box?

  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Getting random number to show up in text box?

    I cant get a random number to show up in my text box.
    PHP Code:
    function Random() {
    var 
    Random Math.floor(Math.random() * 10);

    And here is the html

    PHP Code:
    <input type="text" name="Result" value="" id="Temptext" size="35" />



    <
    input type="button" value="Guess"
    onclick="
    Random();
    var taco = Random().value;
    taco = document.getElementById ('Temptext');
    Random.value = taco;
    "
    /> 
    Any help would be greatly appreciated.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb

    I believe this is what you want ...
    Code:
    <script type="text/javascript">
    function Random() {
      return Math.floor(Math.random() * 10);
    } 
    </script>
    
    <input type="text" name="Result" value="" id="Temptext" size="5" />
    <input type="button" value="Guess" onclick="document.getElementById ('Temptext').value=Random()"/>

  3. #3
    Join Date
    May 2012
    Posts
    9
    suppose I have a table :

    <table name="table" id="table" cellpadding="4" cellspacing="2" border="1" bgcolor="" >
    <tr name="tr" id="tr">
    <th><input type="checkbox" name="allCheck" onclick="selectallMe()"></th>
    <th>Emp ID</th>
    <th>Device</th>
    <th>Feature Status</th>
    <th>Policy</th>
    <th>Password Management</th>
    </tr>
    <tr>
    <% while(rs.next()){ %>
    <td><input type="checkbox" name="chkName" onclick="selectall()"></td>
    <td><input type="text" name="empId" value="<%= rs.getString(1)%> " disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="device" value="<%= rs.getString(2)%>" disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="features" value="<%= rs.getString(3)%>" disabled="disabled" maxlength="60"></td>
    <td><input type="text" name="policyName" value="<%= rs.getString(4)%>" disabled="disabled" maxlength="10"></td>
    <td id="td" name="td"><input type="text" name="password" id="password" value="" ><input type="button" name="Password" value="Password" onclick="document.getElementById('password').value=randomString();"><input type="reset" name="Reset" value="Rseset"></td>
    </tr>
    <% }
    %>
    </table>

    And I want to paste the text value to the every rows' 5th column(i.e Password), could you tel me that how do I insert it.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330
    I don't know enough about the PHP (???) code to be of any help.

    Perhaps some other forum members will know more than I do.

  5. #5
    Join Date
    Nov 2010
    Posts
    1,031
    doesn't seem like a php question to me (but maybe I'm confused).

    If you just want the same random number to be pasted to each input row, it would look something like this:

    Code:
    <body>
    <table name="table" id="table" cellpadding="4" cellspacing="2" border="1" bgcolor="" >
    <tr name="tr" id="tr">
    <th><input type="checkbox" name="allCheck" onclick="selectallMe()"></th>
    <th>Emp ID</th>
    <th>Device</th>
    <th>Feature Status</th>
    <th>Policy</th>
    <th>Password Management</th>
    </tr>
    <tr>
    <% while(rs.next()){ %>
    <td><input type="checkbox" name="chkName" onclick="selectall()"></td> 
    <td><input type="text" name="empId" value="<%= rs.getString(1)%> " disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="device" value="<%= rs.getString(2)%>" disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="features" value="<%= rs.getString(3)%>" disabled="disabled" maxlength="60"></td>
    <td><input type="text" name="policyName" value="<%= rs.getString(4)%>" disabled="disabled" maxlength="10"></td>
    <td id="td" name="td"><input type="text" name="password" id="password" value="" ><input type="button" name="Password" value="Password" onclick="fillFields()"><input type="reset" name="Reset" value="Rseset"></td>
    </tr>
    <% } 
    %>
    </table>
    <script type="text/javascript">
    rand=Math.floor(Math.random() * 10);
    
    
    function fillFields() {
    tab=document.getElementById("table")
      for (var i = 1; i < tab.rows.length; i++) {
      tab.rows[i].cells[5].firstElementChild.value=rand;
    		}
      } 
    </script>
    
    </body>
    if you wanted a diferrent number in each row, the js would look like this:

    Code:
    <script type="text/javascript">
    function Random() {
      return Math.floor(Math.random() * 10);
    }
    
    function fillFields() {
    tab=document.getElementById("table")
      for (var i = 1; i < tab.rows.length; i++) {
      tab.rows[i].cells[5].firstElementChild.value=Random()
    		}
      } 
    </script>

  6. #6
    Join Date
    May 2012
    Posts
    9
    I have written a code for My code(JS) is running properly,but clicking on Password button it filled up all the password text field,Actually I want to fillup each password text filled by clicking it's corresponding Password button,which is not happening in my code.

    @ xelawho > Thanks for reply.And any input on this behalf...if want to share....will be appriciated.

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Exclamation

    Quote Originally Posted by xelawho View Post
    doesn't seem like a php question to me (but maybe I'm confused).

    If you just want the same random number to be pasted to each input row, it would look something like this:

    [CODE]
    ...
    <% while(rs.next()){ %>
    <td><input type="checkbox" name="chkName" onclick="selectall()"></td>
    <td><input type="text" name="empId" value="<%= rs.getString(1)%> " disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="device" value="<%= rs.getString(2)%>" disabled="disabled" maxlength="10"></td>
    <td><input type="text" name="features" value="<%= rs.getString(3)%>" disabled="disabled" maxlength="60"></td>
    <td><input type="text" name="policyName" value="<%= rs.getString(4)%>" disabled="disabled" maxlength="10"></td>
    <td id="td" name="td"><input type="text" name="password" id="password" value="" ><input type="button" name="Password" value="Password" onclick="fillFields()"><input type="reset" name="Reset" value="Rseset"></td>
    </tr>
    <% }
    %>
    ...
    ...
    May not be PHP, but it not much JS either with <% %> tags and variable assignments.

  8. #8
    Join Date
    Nov 2010
    Posts
    1,031
    I think in that case your button should look like this:
    Code:
    <input type="button" name="Password" value="Password" onclick="fillFields(this)">
    and your function should look like this:
    Code:
    function fillFields(el) {
    el.previousSibling.value=Math.floor(Math.random() * 10);
      }

  9. #9
    Join Date
    May 2012
    Posts
    9
    @ xelawho > thanks and for resetting the each text field the value I have written the code :

    function resetvalue(){
    var rst = document.getElementById("password").reset();
    return rst;
    }

    function reset(el){
    el.previousSibling.value=resetvalue();
    }

    but it's not working

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles