Reputation: 127
i'm trying to write this next code. I have an image and a button. i want to change the button's value (title) when the image is clicked. can anyone tell me what's wrong here? (if u find stupid typos ignore it i wansn't copy pasting everything).
and another Q about the script part (maybe related to my problem): does the method request.getParameter(imageId) really returns the function's above parameter?
JSP:
<%
String messageButton = "Click me!";
%>
<%!
public void changeStatusButton(String i_MessageButton)
{
i_MessageButton = "You Clicked The Image!";
}
%>
html head:
<script>
button imageClicked(imageId) {
<% changeStatusButton(request.getParameter(imageId)) %>
}
</script>
html body:
<input type="image" id="greatimage" onclick="imageClicked(id)"/>
<input id="mybutton" type="button" value="<%=messageButton%>"/>
Upvotes: 1
Views: 3970
Reputation: 5516
All the code that is within the scriptlets will be executed on the server side. No java code gets out from the server, all that comes out is HTML, even if they are all in the same JSP. Your JSP code should look like -
<% String messageButton = "Click me!"; %> // server-side code
<script type="text/javascript"> // client side code
function imageClicked() {
document.getElementById("mybutton").value = "You Clicked The Image!";
}
</script>
<input type="image" id="greatimage" onclick="imageClicked();"/> // client side code
<input id="mybutton" type="button" value="<%=messageButton%>"/> // client side code with <%=messageButton%> evaluated at server side.
So when the HTML code is generated, what gets out from the server and rendered in your browser is this -
<script type="text/javascript">
function imageClicked() {
document.getElementById("mybutton").value = "You Clicked The Image!";
}
</script>
<input type="image" id="greatimage" onclick="imageClicked();"/>
<input id="mybutton" type="button" value="Click me!"/>
Even your variable messageButton
wont come to the client side because that is within scriptlet.
Upvotes: 1