Reputation: 2497
I write the following code in simulate.jsp , to receive some String "simulationInfos" from the Server:
<s:property id="simulationInfos" value="simulationInfos" /><br/>
When simulate.jsp has been returned, I can see, that the String simulationInfos existing.
My Question is: How can I pass simulationsInfos to java script?
I tried the following:
<script type="text/javascript">
var data=document.getElementById("simulationInfos").value;
console.log("data is: ", data);
And with jQuery:
var data=$("#simulationInfos").val();
</script>
and I get the following Error in Firebug: Uncaught TypeError: Cannot read property 'value' of null
How can I pass some struts 2 variable to javascript?
Upvotes: 2
Views: 19080
Reputation: 333
If you do not want to display property values use s:hidden tag like this:
<s:hidden id="simulationInfos" value="%{simulationInfos}" name="simulationInfos"/>
<script type="text/javascript">
var data=$("simulationInfos").value;
console.log("data is: ", data);
</script>
Upvotes: 1
Reputation: 3687
First, you should take a look to the HTML code that the JSP is generating. It can explain why you cannot retrieve the value. Then, maybe you will see that you need some extra HTML code. For example:
<span id="simulationInfos"><s:property value="simulationInfos" /></span>
And in you script:
alert("data is: " + simulationInfos.innerHTML);
If you take a look to the property tag doc, you can see that there is not any "id" parameter.
Edition: JS correction.
Upvotes: 3
Reputation: 3742
It looks like your id is simulationInfos but you are looking for simulateInfos in your js code. Since your getElementById is not returning anything, there is no value on a (null) return type.
Also, in console.log, use a + to concatenate your strings, not a comma.
Upvotes: 1