Reputation: 41
I am dynamically creating hidden values on my html page in a loop, and trying to use said values to create a chart, but cannot seem to access with document.getElementByID.
So it gets the name and a number from a servlet. The
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="buttons.css">
<link rel="stylesheet" type="text/css" href="${skin}.css">
<link rel="stylesheet" type="text/css" href="tables.css">
<link href='http://fonts.googleapis.com/css?family=Oswald'
rel='stylesheet' type='text/css'>
<title>Hello ${user}</title>
<title>Radar Chart</title>
<script src="Chart.js"></script>
<meta name="viewport" content="initial-scale = 1, user-scalable = no">
<style>
canvas {
}
</style>
</head>
<body>
<center>
<br>
<div class="container">
<h1>Number of Trades Per Stock Exchange</h1>
</div>
<div id="container">
<table class="zebra">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Number of Trades Made</th>
</tr>
</thead>
<tbody>
<c:forEach items="${first}" varStatus="loop">
<tr>
<td><c:out value="${first[loop.index]}" /></td>
<td><c:out value="${price[loop.index]}" /></td>
<input type="hidden" id="${first[loop.index]}"
value="${price[loop.index]}" />
</tr>
</c:forEach>
</tbody>
</table>
</div>
<br>
<canvas id="canvas" height="450" width="450"></canvas>
<script>
var pieData = [ {
value : document.getElementById("London Stock Exchange"),
color : "#F38630"
}, {
value : document.getElementById("BombayStockExchange"),
color : "#E0E4CC"
}, {
value : document.getElementById("New York Stock Exchange"),
color : "#69D2E7"
}
];
var myPie = new Chart(document.getElementById("canvas").getContext(
"2d")).Pie(pieData);
</script>
<br> <br>
<div id="inline-link-1">
<a href="semanagerpanelredirect">Return to Stock Exchange Manager
panel</a>
</div>
</center>
</body>
</html>
Here is the source code when I load the page:
<html>
<head>
<link rel="stylesheet" type="text/css" href="buttons.css">
<link rel="stylesheet" type="text/css" href="blue.css">
<link rel="stylesheet" type="text/css" href="tables.css">
<link href='http://fonts.googleapis.com/css?family=Oswald'
rel='stylesheet' type='text/css'>
<title>Hello LondonManager</title>
<title>Radar Chart</title>
<script src="Chart.js"></script>
<meta name="viewport" content="initial-scale = 1, user-scalable = no">
<style>
canvas {
}
</style>
</head>
<body>
<center>
<br>
<div class="container">
<h1>Number of Trades Per Stock Exchange</h1>
</div>
<div id="container">
<table class="zebra">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Number of Trades Made</th>
</tr>
</thead>
<tbody>
<tr>
<td>London Stock Exchange</td>
<td>23</td>
<input type="hidden" id="London Stock Exchange"
value="23" />
</tr>
<tr>
<td>BombayStockExchange</td>
<td>1</td>
<input type="hidden" id="BombayStockExchange"
value="1" />
</tr>
<tr>
<td>New York Stock Exchange</td>
<td>9</td>
<input type="hidden" id="New York Stock Exchange"
value="9" />
</tr>
</tbody>
</table>
</div>
<br>
<canvas id="canvas" height="450" width="450"></canvas>
<script>
var pieData = [ {
value : document.getElementById("London Stock Exchange"),
color : "#F38630"
}, {
value : document.getElementById("BombayStockExchange"),
color : "#E0E4CC"
}, {
value : document.getElementById("New York Stock Exchange"),
color : "#69D2E7"
}
];
var myPie = new Chart(document.getElementById("canvas").getContext(
"2d")).Pie(pieData);
</script>
<br> <br>
<div id="inline-link-1">
<a href="semanagerpanelredirect">Return to Stock Exchange Manager
panel</a>
</div>
</center>
</body>
</html>
I can set the values but cannot get them. Any help would be appreciated.
Upvotes: 0
Views: 150
Reputation: 382092
You're passing the DOM elements, not their values.
Replace
value : document.getElementById("London Stock Exchange"),
with
value : document.getElementById("London Stock Exchange").value,
And, as mentionned by Devang Rathod, you should not have spaces in your id (see reference).
Upvotes: 1
Reputation: 13
use document.getElementById("London Stock Exchange").value or you can use simpler jquery. Ex : ("#London Stock Exchange").val();
Upvotes: 0