0

I'm following the answer from add onclick function to a submit button

Here's my HTML:

Name: <input type="text" id="name">
<br>
<br>
<input type="submit" onclick="return process();" />

and JS:

var text = document.getElementById("name").value;

function process() {
    alert(text);
    return true;
}

When I click the form, it says: Uncaught ReferenceError: process is not defined

I have double checked everything, and the process() function is already defined. I wonder why this doesn't work

Here's the DEMO

==================UPDATE================================

I tried it using SO's code snippet, and it worked. I guess there's something wrong with jsfiddle, somehow the javascript and HTML are not conected

function process() {
    var text = document.getElementById("name").value;
  
    alert(text);
    return true;
}
Name: <input type="text" id="name">
<br>
<br>
<input type="submit" onclick="return process();" />

5
  • Are you calling the js file anywhere in your HTML? Commented Nov 7, 2014 at 19:51
  • Yes, please take a look at the jsfiddle demo Commented Nov 7, 2014 at 19:52
  • Where is the js code? in a external file? Are you sure to include this file in the html page? Commented Nov 7, 2014 at 19:53
  • The jscode is there, under the javascript box. Can you not see it from your screen? I can see it from here just fine Commented Nov 7, 2014 at 19:58
  • @Limantara He's asking where you have it in your markup, not where it's at in the fiddle. Commented Nov 7, 2014 at 20:11

2 Answers 2

2

The problem is your order of actions. Your javascript needs to be appended after the DOM loads.

Name: <input type="text" id="name">
<br>
<br>
<input type="submit" onclick="return process();" />

<script>
var text = document.getElementById("name").value;

function process() {
    alert(text);
    return true;
}
</script>

And you should actually include var text within the function then you can load the JS wherever.

Name: <input type="text" id="name">
<br>
<br>
<input type="submit" onclick="return process();" />

<script>

function process() {
    var text = document.getElementById("name").value;
    alert(text);
    return true;
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

@epascarello correct, if you use var text within the function that will work. I answered based on your current order of operations.
@epascarello woops sorry I thought you were the original poster.
1

The error message you get is due to the way Jsfiddle.net works. Tested in isolation, the code runs without errors. However, the alerted text is the empty string. The reason is that the variable text is assigned only once, at the start of execution, and then the input field is empty.

One way to fix this is to make the text variable local to the function, so that it will be assigned a value whenever the function is invoked:

function process() {
    var text = document.getElementById("name").value;
    alert(text);
    return true;
}

1 Comment

I just figured that out myself too! Apparently it works just fine with SO's code snippet. Oh yeah, I should have put the text inside of the function. Anyway, thanks

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.