[javascript] how to set value of a input hidden field through javascript?

I am trying to call the resetyear function and its getting called also but the flow stops at alert("over"), it doesnt tranfer its control to resetmaster. Please suggest me something.

    String flag = "";
    flag = (String) session.getAttribute("flag");
    System.out.println("flag = " + flag);
    if (flag == null) {
        flag = "";
    }
    if (flag.equals("yes")) {
%>   
<script>

   alert(1);
  // resetyear();
    dontreset();
    //document.getElementById("checkyear").value = "1";
    //alert(document.getElementById("checkyear").value);

</script>
<%} else if(flag.equals("no"))
    {%>
<script>
    alert(2);
    //document.getElementById("checkyear").value = "2";
    //alert(document.getElementById("checkyear").value);
    resetyear();
</script>
<%}else{}%>


function resetyear(){

if(confirm("DO YOU WANT TO RESET THE YEAR?"))
{
    alert("hello");
    //document.forms['indexform'].action = "resetmaster";
    //alert(document.forms['indexform'].action);
    //document.forms['indexform'].submit();  
    alert("over");
    form.action = "resetmaster";
    form.submit();
    alert(1);
}

This question is related to javascript html hidden-field

The answer is


For me it works:

document.getElementById("checkyear").value = "1";
alert(document.getElementById("checkyear").value);

http://jsfiddle.net/zKNqg/

Maybe your JS is not executed and you need to add a function() {} around it all.


It seems to work fine in Google Chrome. Which browser are you using? Here the proof http://jsfiddle.net/CN8XL/

Anyhow you can also access to the input value parameter through the document.FormName.checkyear.value. You have to wrap in the input in a <form> tag like with the proper name attribute, like shown below:

<form name="FormName">
    <input type="hidden" name="checkyear" id="checkyear" value="">
</form>

Have you considered using the jQuery Library? Here are the docs for .val() function.


The first thing I will try - determine if your code with alerts is actually rendered. I see some server "if" code in what you posted, so may be condition to render javascript is not satisfied. So, on the page you working on, right-click -> view source. Try to find the js code there. Please tell us if you found the code on the page.


You need to run your script after the element exists. Move the <input type="hidden" name="checkyear" id="checkyear" value=""> to the beginning.


Your code for setting value for hidden input is correct. Here is the example. Maybe you have some conditions in your if statements that are not allowing your scripts to execute.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to hidden-field

yii2 hidden input value how to set value of a input hidden field through javascript? how to pass data in an hidden field from one jsp page to another? Set the Value of a Hidden field using JQuery rails simple_form - hidden field - create? JQuery: Change value of hidden input field jQuery - Create hidden form element on the fly