1

I have a hidden field

<input type="hidden" name="smname" />

Now i have a function in a js file count(); How can I assign the return value to the hidden field?

4 Answers 4

14

You could give your hidden field an unique id:

<input type="hidden" name="smname" id="smname" />

and then in javascript:

document.getElementById('smname').value = count();

where count is the function you want to invoke and which returns the value:

var count = function() {
    return 'some value';    
};
1
  • Or with JQuery: $("#smname").val( count );
    – Musky
    Jul 10, 2018 at 21:37
2

Try:

document.getElementsByName('smname')[0].value = count();
0

You want to use code such as

document.getElementById("name")

then deal with it as an object. remember to define a name/id for your html element.

0

If you give the input element an id attribute you will be able to get a reference to it in javascript using the document.getElementById() method then set the value property of the element

HTML

<input id="surname" type="hidden" name="smname" />

Javascript

var input = document.getElementById("surname");
input.value = count();

Your Answer

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