set value in a input field with javascript

5 replies
How would you set the value in a form input field with javascript. I tried by my own but it is not working. can anyone correct it.

Incorrect Coding:

<script type="text/javascript">
var a = document.getElementById("name");
a.value = "some value";
</script>


<input type="text" id="name">

#field #input #javascript #php learning help #php training #set
  • Profile picture of the author fordix
    Edit fiddle - JSFiddle

    Seem to work fine, you probably have some other issue in your code, unrelated to the one which you have posted. can you create a fiddle so we all be able to see?
    {{ DiscussionBoard.errors[8872856].message }}
    • Profile picture of the author mlcmartin
      $("input[name='fieldname']").val("value");
      {{ DiscussionBoard.errors[8900989].message }}
      • Profile picture of the author Zdenek Koukol
        Originally Posted by mlcmartin View Post

        $("input[name='fieldname']").val("value");
        Yes, use jQuery ... it will make your live much easier. And btw, how do you debug your Javascript code? I use Firefox with FIREBUG, if you have issue in your code, then FIREBUG will help you to localize it.
        Signature

        Check my website ... http://www.becomerichforever.com

        {{ DiscussionBoard.errors[8903065].message }}
  • Profile picture of the author shahriyar
    Originally Posted by Luca tall View Post

    How would you set the value in a form input field with javascript. I tried by my own but it is not working. can anyone correct it.

    Incorrect Coding:

    <script type="text/javascript">
    var a = document.getElementById("name");
    a.value = "some value";
    </script>


    <input type="text" id="name">

    Your code looks alright, there must be other code that causing error. Post your URL so we can take a look.
    {{ DiscussionBoard.errors[8903739].message }}
  • Profile picture of the author yakim1
    Originally Posted by Luca tall View Post

    How would you set the value in a form input field with javascript. I tried by my own but it is not working. can anyone correct it.

    Incorrect Coding:

    <script type="text/javascript">
    var a = document.getElementById("name");
    a.value = "some value";
    </script>


    <input type="text" id="name">

    I believe your input is incomplete. It should be...

    <input type="text" id="name" value="some value">

    Best regards,
    Steve Yakim
    {{ DiscussionBoard.errors[8920545].message }}

Trending Topics