43

I am trying to call the resetyear function and it's getting called also, but the flow stops at alert("over"). It doesn't transfer its control to resetmaster.

    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);
}
wuerfelfreak
  • 2,118
  • 1
  • 10
  • 24
Divyang
  • 511
  • 2
  • 8
  • 18
  • try putting your html *before* the script thats trying to act on it. What is the "" for? – underscorePez Oct 07 '13 at 19:26
  • @underscorePez thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:17
  • 1
    Have the edits made the question completely divorced from the title and answers? – Teepeemm Feb 23 '18 at 16:28

5 Answers5

76

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.

almo
  • 5,249
  • 3
  • 33
  • 70
  • But then I will have to call that function which i don't want . i want it to set based on if condition. – Divyang Oct 07 '13 at 19:31
  • if you make just function() {} (without a name for the function) then the function is called onload. – almo Oct 07 '13 at 19:32
  • at least when i use jquery it works like this: $(function() {}); – almo Oct 07 '13 at 19:35
  • thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:19
6

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

Matt H
  • 195
  • 1
  • 8
  • thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:19
6

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.

Tadhg
  • 175
  • 1
  • 4
  • 15
andreacanton
  • 333
  • 3
  • 11
  • thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:19
  • 1
    .val() is unable to act on hidden elements. See https://stackoverflow.com/questions/2979772/set-value-of-hidden-field-in-a-form-using-jquerys-val-doesnt-work – zamber Apr 03 '14 at 16:19
  • 1
    are you sure, zamber? I don't think so https://jsfiddle.net/rcegvttm/ (I used jquery 1.12, try also with 2.x or 3.x works anyway) another post: http://stackoverflow.com/questions/4376664/jquery-access-input-hidden-value – andreacanton Feb 15 '17 at 15:04
1

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.

Alex Buyny
  • 2,498
  • 14
  • 23
  • thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:20
1

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.

Alex
  • 1,457
  • 1
  • 11
  • 6
  • thank you buddy. I changed the whole code. Instead of using hidden tag, I have directly called the function from the if condition. I have again modified the question. Please check and suggest me answer – Divyang Oct 08 '13 at 19:20