floribon
floribon

Reputation: 19193

How to change a hidden input value just before submitting using javascript?

I am a very beginner in javascript and I would like to change a single input value when the user selects an option with a form, thhen submit it to the same page.

here is the Html code:

<form id="form" action="thispage.jsp" method="post">
<input id="action_type" type="hidden" name="action_type" value="firstAction" />
<table class="choose">
    <tr>
        <td>Choose your test</td>
        <td><select id="select_test" name="test_name">
            <option value="test0"></option>
            <option value="test1"></option>
        </select></td>
    </tr>
</table>

And the javascript function:

form = document.getElementById("form");
action = document.getElementById("action_type");
select = document.getElementById("select_test");
select.onchange = function() {
    action.setAttribute("value", "otherAction");
    form.submit();
}

And this returns an Exception, what is wrong with this code ?

Upvotes: 0

Views: 626

Answers (3)

Russell Gutierrez
Russell Gutierrez

Reputation: 1385

I think nullPointer shows because there is no selected option. Since the code does not have any innerHTML on it, you cant choose anything.

Upvotes: 0

AndreCruz
AndreCruz

Reputation: 663

It doesn't seem to be a problem with JavaScript, but with your JSP code. Try debugging the code and check where it's failing.

Upvotes: 1

Fabian Blechschmidt
Fabian Blechschmidt

Reputation: 4141

I think the form is not defined. try to add a var before the form.

Upvotes: 1

Related Questions