Reputation: 31
I have a JSP with HTML input fields like "username"
. I have a JavaScript method where I get the value of this field using `document.getElementById('userId').value.
Now I have a struts2 tag: <s:hidden name="username" />
In my JavaScript, before submitting, I need to set the value from the input text field to the Struts 2 hidden tag field. I cannot use the text directly from the input field and I need to set it to the hidden Struts 2 tag.
Upvotes: 3
Views: 19599
Reputation: 5327
First you have to give id to your hidden field then you can set it with jquery or javascript.
Following is example to set it with jquery
This is your form
this will set username from jquery;
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click', '#s', function(event) {
event.preventDefault();
var name="Ram";
$("#uname").val(name);
});
});
</script>
If it is very important data then save it to session or any other scope and get it directly to action dont show it to jsp page
Upvotes: 2
Reputation: 31
You can set the value of a textfield to the hidden value on submit button.
Let to show you an example,
<script type="text/javascript>
function setHiddenValue(){
document.getElementById('hiddenfield').value = document.getElementById('textfield');
}
</script>
<s:hidden name="hiddenfield" id="hiddenfield" />
<s:textfield name="textfield" id="textfield" />
<input type="submit" onclick="setHiddenValue();" />
Upvotes: 1
Reputation: 23587
Struts2 tags only provides some additional functionality to work better with Struts2 flow.They are like helpers which give you some out of the box features to work closely with struts2.
In the end when your JSP will be displayed by the browser be it struts2 tag/your custom tags or any other tag library you are using will be converted in to pure HTML which is understood by your browser.
So if you have a text-field say
<s:textfield name="username" id="username" value=""/>
it will be converted in to HTML at final display like
<input type="text" name="username" id="username" value="">
Just note that if you will not provide id
for your textfield struts2 will generate the id by itself while converting the tag code to final HTML so if you want your custom id for the textfield/any other field give it explicitly.
So you have all way to use famous javascript way
document.getElementById("myhidden")
and do what ever you want to do with the values.
Upvotes: 0
Reputation: 8016
Struts2 input tags are finally converted into the normal HTML tags on page rendering, so there is nothing special you need to assign them a value. Just mention a id to the hidden element and assign the value using the javascript like you usually do
<s:textfield id="mytext" value="yourvalue"/>
<s:hidden id="myhidden" value=""/>
function assignvalue(){
document.getElementById("myhidden").value=document.getElementById("mytext").value;
}
Upvotes: 1