0

Is it possible to directly use the result of a Javascript function as an input value for an HTML form?

<form class="my-form" action="fileUpload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="User_id" value="getValue();">
    <input type="submit" value="Submit"/>
</form>

This is clearly not correct, but I hope it communicates what I am trying to do. I would like the returned result of JS function getValue(); to act as the input value.

2
  • Do you want to set a value with JavaScript? Commented Jun 13, 2016 at 1:09
  • Yes - In my specific case I built a webapp using Axure (prototyping tool) and using a javascript function is the only way I know how to access the local Axure variables. Commented Jun 13, 2016 at 1:16

2 Answers 2

1

In that implementation, no it is not possible to equate the value attribute inside the input tag to a JavaScript function.

According to w3.org, the value attribute can only equal

String without line breaks

Any string that contains no line feed (U+000A, “LF”) or carriage return (U+000D, “CR”) characters.

I am not sure what is inside the getValue() function, but you can call a click event handler when the submit button is clicked and run that getValue() function.

Ex.

<form class="my-form" action="fileUpload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="User_id">
    <input type="submit" value="Submit" onclick="getValue();"/>
</form>

I hope it guides you well.

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

2 Comments

Thanks! The "getValue();" function simply returns a number. In your suggestion, the function is being called, but what I really need is for the returned value of that function to serve as the value for the User_id input, which I don't think is happening in your response.
I see, then what you can do is what JonSG has suggested below, after adding a click event listener, set the value of input[name='User_id'] to 'getValue();'
0

While it is technically possible to do something like this:

<script>
  function getValue(){ return "foo"; }
  document.write("<input type='hidden' name='User_id' value='" + getValue() + "' />");
</script>

That is typically frowned upon as there are scoping issues and page rendering implications. The more accepted way would be to do something more like this..

function getValue(){ return "bar"; }

window.addEventListener("load", function(event){
    document.querySelector("input[name='User_id']").value = getValue();
});

or potentially at form submit or submit button click with something like:

function getValue(){ return "bar"; }

document.querySelector("input[type='submit']").addEventListener("click", function(){
    document.querySelector("input[name='User_id']").value = getValue();
});

Though I am not keen on it myself, it would be possible to do the same as above with:

<input type="submit" value="Submit" onclick="setUserId();"/>

Then later in say a end of body script block do:

function getValue(){ return "bar"; }

function setUserId(){
    document.querySelector("input[name='User_id']").value = getValue();
}

4 Comments

Thanks for the reply - In regard to your last suggestion, where would I place that in my code? Directly after the submit button declaration, still inside the form? Also, would this update the value for "User_ID" before the form was executed in the fileUpload.php file?
I would place it at the bottom on the body myself. I try to keep scripting out of what might be thought of as the "rest of the html". If you want a more "inline" option, I'll update with a 4th block...
Gotcha. So would I start by declaring this as blank: <input type="hidden" name="User_id" value=""> and having the event listener update the value on the triggered submit button click?
@Jwarzy72 Ya, exactly. You might even set it to something like "IdNotSet" then test for that server side (or test for "" if you would rather)

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.