Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Passing values from HTML form <---> JavaScript?

Hello. I was wondering how to pass values/variables back and forth from my HTML form to a JavaScript function and back to HTML form?

Lets say my form get's a users telephone number as three parts...

<input type="text" name="areacode" value="800" maxlength="3">

<input type="text" name="prefix" value="555" maxlength="3">

<input type="text" name="number" value="1212" maxlength="4">

Now I want to make a JavaScript function to take those 3 values and give me a...

var phone_no="("+areacode+") "+prefix+"-"+number

Then I want to pass (phone_no) back to the form as something like...

<input type="hidden" name="phone_no">

How can I accomplish this? I need for a few different scripts. After all that, is there another way to combine those without js? Can I somehow make.

<input type="hidden" name="phone_no" value='"("+areacode+") "+prefix+"-"+number'>

Update:

so...

document.formname.areacode.value

document.formname.prefix.value

document.formname.number.value

... gets input data from html form, and...

document.formname.phone_no.value

... send phone_no back to form?

Update 2:

BillyZ313, please complete your link with an actual discussion, not just dump me in the top forum. Thank you.

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You could write the following:

    function updatePhoneNo() {

    document.formname.phone_no.value = "("+document.formname.areacode.value+")"+document.formname.prefix.value+"-"+document.formname.number.value;

    }

    The form would benefit by having a name and you would replace "formname" with what you set.

    Then I would probably call this function with a "onChange" event for the input fields to update the phone_no field.

    You might also want to look into validating the input like to see if they enter 2 numbers or 3 into the fields.

  • 1 decade ago

    Aha, well basically you can call the values from JavaScript. Here 's a little code that will do the job :D

    - note you'll have to add a <FORM> tag....

    I putted the <FORM name="f1">

    <script>

    function getValue(inputName)

    {

    return f1.[inputName];

    }

    </script>

  • Anonymous
    5 years ago

    assume the form element has the id attribute of "bubbaField" like so <input type="text" id="bubbaField" /> the code to access it could go like this var myVariable = document.getElementById("bubbaField"); //(now myVariable refers to the text box) //to refer to what's in the text box, do this:) var theValue = myVariable.value; // plus many other ways to get the value magic

  • 1 decade ago

    start by giving the elements id=" " attributes, give the same id as the name. then just call the following function it will take care of everything

    function GrabPhoneNumber()

    {

    var areacode = document.document.getElementById("areacode").value;

    var prefix = document.document.getElementById("prefix").value;

    var number = document.document.getElementById("number").value;

    var phone_no="("+areacode+") "+prefix+"-"+number;

    document.document.getElementById("phone_no").value = phone_no

    }

    for more help stop by http://www.billyzteksupport.com/

  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    I use PHP

    Something like

    $PhoneNumber= "$_POST['areacode']-$_POST['prefix']-$_POST['number']";

Still have questions? Get your answers by asking now.