0

Is there a way to use the value from a text field as the parameter value in an onclick statement for a button. For example if you have some text field named "name" and you have another button named "button" when clicked will go to the same page "page.jsp" with a parameter "par" equal to the text field value. This is what I am currently doing:

out.print("<input type = 'button' name = 'noName' value = 'Click Me' onclick =" + '"' + "window.location.href = 'http://page.jsp?par=" + document.form.name.value + "'" + '"');

But for some reason it doesn't like the par=" + document.form.name.value

Any suggestions?

0

2 Answers 2

3

Like this?

<script>
    function doSomething(txt){
        alert(txt);   //alert the text maybe?
        //do more things...
    }
</script>
<input onClick="doSomething(this.value)">

this.value means the value in the textfield.

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

1 Comment

Thanks, I figured out what I was doing run.
0
String s = "<input type='button' name='noName' value='Click Me' onclick="
         + "\"window.location.href='http://page.jsp?par="
         + document.form.name.value + "'\"";

Well, that's pretty confusing.

document etc. has zero meaning on the server side. Why aren't you doing this in JavaScript, where it would be a lot easier? Why isn't this in the JSP, where it would be trivial?

If you want to access a form value from a servlet, (a) that form value must have been submitted, and (b) you access it via request.getParameter("parameterName"). But I find it impossible to believe that's the easiest way to do it.

Comments

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.