Eswar Eswar - 1 year ago 85
Javascript Question

Is possible : javascript extract value from c:forEach tag?

i have populate some values using c:forEach tag. I want to get those values in my javascript.

If I click GetCtag value button, then i want to read from (c:forEach) values in javascript.

Is any other-way to retrieve the c:forEach tag value

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="h" %>
<%@ taglib uri="" prefix="f" %>
<%@ taglib uri="" prefix="a4j"%>
<%@taglib prefix="c" uri="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">

function getCTagValue(ctagObject)
alert("CFor Each Tag Object Value: " + ctagObject);
// Here i want write code for retrieve the c:forEach tag value

<h:form id="cTagForm" >

<c:forEach items="${cTagBean.tagList}" var="ctag">
<c:out value="${} : "/>
<c:out value="${ctag.age}"/></br>

<a4j:commandButton id="GetCtagId" value="GetCtag" oncomplete="getCTagValue('#{cTagBean.tagList}')"/>


Help me.
Thanks in advance.

Answer Source

Just print it in JavaScript syntax instead of HTML syntax.

    var data = {
        <c:forEach items="${cTagBean.tagList}" var="ctag" varStatus="loop">
            '${}': ${ctag.age}${!loop.last ? ',' : ''}

So that it end up as valid JavaScript object (assuming that name returns String and age returns Number):

    var data = {
        'foo': 10,
        'bar': 5,
        'waa': 20
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download