How to pre-populate form field with javascript script!?

by 7 replies
14
Hey,
I am setting up a form for an online store, and need the title of the website to turn up into one of the fields, I don't want to type out each website title so I am wanting javascript to do it for me.

Here is the code I am using for the specific field...

<tr>
<td valign="top">
<label for="product">Product *</label>
</td>
<td valign="top">
<input type="text" name="product" maxlength="300" size="30">
</td>
</tr>

I need it to pre-populate with this script...

<script>
document.write(document.title);
</script>

...which grabs the website title.

However everyway I have tried of entering that just comes up with that as text instead of executing and showing up with the page title.

I am really stuck on this so any help would be awesome!

Cheers,

Charlie.
#programming #field #form #javascript #prepopulate #script
  • Code:
    window.onload = function() { 
      document.forms['FormName'].elements['txtName'].value = document.title;
    }
    • [ 1 ] Thanks
    • [1] reply
    • You can use following cod for it


      HTML Code:
      function pop()
      {
      var fname;  
      fname= document.forms['FormName'].elements['txtName'].value = document.title;
      alert(fname);
      
      }
      • [1] reply
  • idia using php
  • Why are you not using Jquery ???

    With Jquery, the code for filling a text input with id 'input1' becomes:

    • [ 1 ] Thanks
  • Here is a working example:
    Code:
    <!DOCTYPE html>
    <html lang='en'>
    <head>
    <title>Fairy Dust</title>
    <!-- <meta http-equiv='X-UA-Compatible' content='IE=edge'> -->
    </head>
    <body>
    <form name="frm" id="frm">
    <table>
    <tr>
    <td valign="top">
    <label for="product">Product *</label>
    </td>
    <td valign="top">
    <input type="text" name="product" maxlength="300" size="30" /> 
    </td>
    </tr>
    </table>
    </form>
    <script>
    document.frm.product.value=document.title;
    </script>
    </body>
    </html>
    Have a Great Day!
    Larry Keenan
    • [ 1 ] Thanks
  • Awesome thanks heaps guys! This sorted it out perfectly!

Next Topics on Trending Feed