1

I am using the following JavaScript code to set the value of an element.

document.getElementById('alert['+i+']').innerHTML = "alert";

The element is nested in a Struts set tag. (The ID match up, the fault is not there.)

<s:set var="alert" >
   <span id="<s:property value='"alert"+{#stat.index+4}' />"></span>
</s:set>

the set tag var is used by a Struts if tag.

<s:if test="%{#alert == 'alert'}">
    //some code         
</s:if>

when the span tag is outside of the set tag, the value of the span tag is set to "alert" correctly. However when inside the struts set tag. the span tag cannot be found.

How do I successfully set the value of the set tag? either using the span tag or setting the value of the set tag directly. Or can I somehow inject the "alert" value in the if tag directly?

I have used google and youtube to find answers. I have tried to put the span tag directly in the if tag. I have tried using document.querySelector() but I couldn't get it to work with strut tags.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MicSar
  • 21
  • 3

1 Answers1

1

If you are using document.getElementById() in JavaScript code, then the element with the id should be available in the DOM. But it's not available because you put it in the Struts <s:set> tag. It's not UI tag and it doesn't render any HTML elements.

You can set the value of alert variable in the value attribute. It allows to apply an OGNL expression there.

<s:set var="alert" value="%{'alert'+(#stat.index+4)}"/>

<span id="<s:property value='%{#alert}'/>">
</span>

Then if you use <s:if> tag the value of the variable alert should be found in the context.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • If this answer helped you then you should mark it as accepted and upvote if it's possible. The checkbox and vote up controls are on the left of the answer. – Roman C Nov 25 '22 at 16:34