broun
broun

Reputation: 2593

Accessing attribute value of tag in a javascript within the custom tag

I have a tag file which defines a bunch of attributes. I want to be able to access the attribute inside a script defined inside the custom tag.

sample.tag


<@tag language="java" pandeEncoding="UTF-8"%>
<%@ attribute name="dummy" required="false" type="java.lang.String" %>

<script>
 console.log(  "value:  " + dummy );
</script>

Not sure im asking something very obvious, but all the google result talks about retrieving an attribute from a tag but i want to get it within the tag definition. So do not have the tag id or name.

Upvotes: 0

Views: 756

Answers (1)

Gummyball
Gummyball

Reputation: 220

Try this:

<%@ tag language="java" pageEncoding="UTF-8" %>
<%@ attribute name="dummy" required="false" type="java.lang.String" %>

<script>
 console.log(  "value:  ${dummy}" );
</script>

If your attribute is user controlled input, it's better to escape your attribute. The JSTL core library can help:

<%@ tag language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="dummy" required="false" type="java.lang.String" %>

<script>
 console.log(  "value:  <c:out value="${dummy}" /> );
</script>

Upvotes: 1

Related Questions