0

Hi there I am having an issue in JSP where I wish to call a javascript function:

<c:forEach var='item' items='${bookLogs}' varStatus="status">
...

<tr >
    <td colspan="9"><a href="javascript:expandOrCollapse(${sectionNumber}, ${item.type.name});">
</tr>

...
</c:forEach>  

Currently I firebug gives me an error: Returned is not defined. javascript:expandOrCollapse(1,%20Returned);()

How do I pass in just the textual value of item.type.name ???

1
  • Quote it.. &quot;${item.blabla}&quot;. Commented Jun 29, 2012 at 17:10

3 Answers 3

1

Apparently, ${item.type.name} is a string and therefore needs to be escaped and quoted, properly. If, for example ${item.type.name} is the string returned "value" & more, your HTML output should look like this:

<a href="javascript:expandOrCollapse(1, &quot;returned \&quot;value\&quot; &amp; more&quot;);">
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put quotes around the string or it will be treated as a javascript variable name:

<a href="javascript:expandOrCollapse(${sectionNumber}, '${item.type.name}');">
//                                                     ^     quotes      ^

You'll need to quote sectionNumber as well if it is a string. if it is a number, you don't.

4 Comments

This seems to fix it on that page. But what gets displayed from the javascript running is "Returned". So it seems "Returned" the string is being passed in.
@Ace i'm not sure what you mean, what behavior are you expected and what is it doing?
on the side of the javascript is was receiving the string "Returned" instead of the string that was contained in ${item.type.name}
@Ace that means item.type.name has the value 'Returned', are you sure you're using the right property of your object?
0

The solution I came up with is a combination of jbabey and Salman A's answers. I added the quotes like jbabey suggested. But it sill needs to be HTML escaped like Salman A said, so I added f:escapeHtml function preceding the parameter and that worked.

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.