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

7 replies
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.
#field #form #javascript #prepopulate #script
  • Profile picture of the author IdeaBox
    Code:
    window.onload = function() { 
      document.forms['FormName'].elements['txtName'].value = document.title;
    }
    {{ DiscussionBoard.errors[8480477].message }}
    • Profile picture of the author marketkidz
      You can use following cod for it


      HTML Code:
      function pop()
      {
      var fname;  
      fname= document.forms['FormName'].elements['txtName'].value = document.title;
      alert(fname);
      
      }
      Signature
      Are you still struggling to grow your Internet business income?
      Learn the secrets that increased my income 700%--and can do the same for you--in my free report "5 Business Prosperity Secrets."
      {{ DiscussionBoard.errors[8480757].message }}
      • Profile picture of the author IdeaBox
        Originally Posted by marketkidz View Post

        You can use following cod for it


        HTML Code:
        function pop()
        {
        var fname;  
        fname= document.forms['FormName'].elements['txtName'].value = document.title;
        alert(fname);
        
        }
        Why call a second function when you can do it right in the onload event?
        {{ DiscussionBoard.errors[8481644].message }}
  • {{ DiscussionBoard.errors[8480983].message }}
  • Profile picture of the author Jay27
    Why are you not using Jquery ???

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

    $(function(){
    $('#input1').val('your text here');
    });
    Signature
    Build Your Widget - Claim Your Free Full Access Lifetime Membership Now!
    {{ DiscussionBoard.errors[8485106].message }}
  • Profile picture of the author LarryKeenan
    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
    {{ DiscussionBoard.errors[8491349].message }}
  • Profile picture of the author charliemwallace
    Awesome thanks heaps guys! This sorted it out perfectly!
    {{ DiscussionBoard.errors[8491414].message }}

Trending Topics