1

I'm trying to make a form which accepts values from the user and then passes those values to a JS function in the same file for some calculations. I'm just trying to print the values to the console right now, but I keep getting the error "Object #<HTMLDocument> has no method 'getElementByID'". Here is the code:

<!DOCTYPE html>
<html>
<body>

<form action="javascript:formHandler();" method="get">
    <h1 align="center">Set the parameters you would like to visualize</h1>

    Center Frequency: <input type="text" name="cf" id="cf"><br>
    Bandwidth: <input type="text" name="bw" id="bw"><br>
    Number of Bins: <input type="text" name="bins" id="bins"><br>
    Number of Values to be Visualized: <input type="text" name="values" id="values"><br>
    <input type="submit" value="Submit">
</form>
<script>


    function formHandler()
    {
            console.log(document.getElementByID("cf")); // This is where I'm getting the error
    }

</script>
</body>
</html>
4
  • what does it mean transfer data from an HTML form to a JavaScript function ? Commented Aug 2, 2013 at 16:38
  • I'm trying to send the center frequency, bandwidth, bins, and values to the JavaScript function. I want to log the center frequency to the console for now. Sorry for the bad phrasing. Commented Aug 2, 2013 at 16:41
  • I think, you are trying to retrieve textbox value when function called, is it ? Commented Aug 2, 2013 at 16:42
  • Yeah, that's exactly it. Commented Aug 2, 2013 at 16:43

3 Answers 3

2

your document.getElementByID should have a lowercase d at the end and you should also add .value, like so

document.getElementById("id").value

to get the value of whats being pass or typed

Sign up to request clarification or add additional context in comments.

4 Comments

I tried what you suggested in combination with what jWeaver suggested, and it works, but the value only appears in the console for a split-second. Is that supposed to happen?
your form is submitting so the page is being reload. in your form add this action="javascript:formHandler(); return false;"
What if I wanted to store the value in a variable and not have the value disappear once the page reloads?
once the page reloads everything resets, that includes variables. if you wanna save the values that are passed or type i suggest using a database or passing them to global variables like POST or GET then grabbing it again when the page loads
2

There is a typo error. It should be getElementById and not getElementByID.

You can print the value to console as below:

console.log(document.getElementById("cf").value);

Comments

1

Better if you do like this

<form action='somepage.html' OnSubmit="return formHandler();" method="get">
<h1 align="center">Set the parameters you would like to visualize</h1>

Center Frequency: <input type="text" name="cf" id="cf"><br>
Bandwidth: <input type="text" name="bw" id="bw"><br>
Number of Bins: <input type="text" name="bins" id="bins"><br>
Number of Values to be Visualized: <input type="text" name="values" id="values"><br>
<input type="submit" value="Submit">
</form>


<script type='text/javascript'>
function formHandler()
{
        console.log(document.getElementById("cf").value);
  return true;
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.