简体   繁体   中英

Java List element is undefined inside anchor tag

There is a list defined as:

List<String> list = Arrays.asList("Abc", "Def");

The problem is inside anchor tag the list element is coming as undefined.

for (int i = 0; i < list.size(); i++) {
  <a id="<%=list.get(i)%>" href="javascript:;" onClick="openWindow(<%=list.get(i)%>);" align="left">Proceed</a>
 }

The openWindow function is defined as below:

function OpenHierarchyWindow(id) {
  alert("id value "+id.value);
}

The alert function is giving output as undefined. Why the list.get(i) is unable to fetch the data inside the anchor tag.

here JSP scriplets inside onClick javascript function should be quoted. You can replace

onClick="openWindow(<%=list.get(i)%>);"

With

onClick="openWindow('<%=list.get(i)%>');"

Below is complete code for the same.

<%@ page import="java.util.List"%>
<%@ page import="java.util.Arrays"%>

<!DOCTYPE html>
<html>
<head>
<title>JSP Scriplets And Java Script</title>

<script type="text/javascript">
    function OpenHierarchyWindow(id) {
        alert("id value " + id);
    }
</script>

</head>
<body>

    <%
    List<String> list = Arrays.asList("Abc", "Def");
    
    for (int i = 0; i < list.size(); i++) {%>
    <a id="<%=list.get(i)%>" href="javascript:;" onClick="OpenHierarchyWindow('<%=list.get(i)%>');" align="left">Proceed</a>
    <%}%>
</body>
</html>

Hope this helped !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM