I have the need to create a two dimensional array to collect values in my jsp. In order to populate the 2-d array I'm using code like this
<script language="JavaScript">
var row = 0;
var multiDimenArray = new Array();
</script>
<c:forEach var="items" items="${item}" varStatus="status">
............
<script language="JavaScript">
multiDimenArray [row] = new Array();
var column = 0;
row++;
</script>
<c:forEach var="nestedItems" items="${nestedItem}" varStatus="status">
............
<script language="JavaScript">
multiDimenArray [row][column] = "some value";
column++;
</script>
However in the third script block when i attempt to assign the value i get a JS error saying that the variable row is undefined. Is it possible to pass variables between script blocks like this in JS?
You are getting that because you are incrementing row before the third script runs.
So when you try to run multiDimenArray [row][column] = "some value"; row actually equals 1 instead of 0.
You need to move the row++ to after the column is assigned.
Related
I am trying to use the label to display values in a list.
For example: apple, 500kg, $3000 using these label I will display in a list.
to retrieve labels in javascript I used:
<script type="text/javascript">
function listEE(json) {
var ListTagEE= "";
for (var k = 0; k < json.feed.category.length; k++)
{
ListTagEE += ""+json.feed.category[k].term+""; }
var listing = ""
+ListTagEE+
"" ;
document.write(listing);
}
</script>
<!-- ######### Invoking the Callback Function ############# -->
<script type="text/javascript" src='https://someadresss.blogspot.com/feeds/posts/default?alt=json-in-script&callback=listEE'>
</script>
And this is code I used to replace labels with my specific values.
<script>
function lebel23_logo(etiqueta23) {
ratstok = new Array();
ratstok[1] = "Apple Peter"
ratstok[2] = "Mango"
ratstok[3] = "Pine Apple"
if (etiqueta23 == "Apple") {document.write(ratstok[1]);}
if (etiqueta23 == "Mango") {document.write(ratstok[2]);}
if (etiqueta23 == "Pineapp") {document.write(ratstok[3]);}
}</script>
earlier I was using blogger < b: loop> label tag to retrieve label. and used following code.. to display the output
lebel23_logo("<data:label.name/>");
Now I am retrieving labels in javascript through above code which is working. but not able to change label and display it with above function lebel23_logo.
Uncaught TypeError: lebel23_logo is not a function on line 9
https://js.do/helloaaa/asasqqq
sorry, my English and javascript is not that good. i hope I made this post understandable.
Replace ""+json.feed.category[k].term+"" with +lebel23_logo(json.feed.category[k].term)+
https://js.do/MustaphaBouh/asasqqq
I am trying to append a ArrayList to a JavaScript String variable using foreach loop but for some reason it doesn't seem to work. I have been trying to fix this problem for 2 days, but no luck yet. Can anyone help with this? Here's the code.
<div id="graphdiv"></div>
<script type="text/javascript">
var s = "";
<c:forEach items="${dateAndWaitTimes}" var="item">
s.append(${item});
</c:forEach>
g = new Dygraph(document.getElementById("graphdiv"), s);
</script>
Here dateAndWaitTimes contains the ArrayList of String, returned by the controller.
-
Output that I am getting:
Expected Output:
It seems you're getting array values correctly but nothing display correct.
one solution might be : put Java arraylist values into JS arrayList and then print JS arrayList values.
Code :
<script language="javascript">
$(function(){
var values = new Array();
<c:forEach var="item" items="${dateAndWaitTimes}" varStatus="status">
values.push("${item}");
</c:forEach>
alert(" VALUES => "+values[0]);
alert(" VALUES => "+values[1]);
//alert(" VALUES => "+values[2]);
});
I have a php class that set values and updates them. I use getters methods to access those varibles from a javascript function and show them in screen. My problem is when I launch that javascript code, my vars are shown allways with the first values they get. I've checked them and they are up to date when I call myCreateFunction(); but is like this function launches before the php vars are updated. I'm new in javascript and some help would be appreciated, thanks!
<?php
class TableRows {
public $offerId = 0;
public function setOfferId(){
$this->offerId ++;
}
public function getOfferId(){
$this->offerId;
}
}
$tb = new TableRows();
?>
<script>
function myCreateFunction(string) {
var offerId = '<tr><th>'+<?php echo json_encode($tb->getOfferId()); ?>+'</th></tr>';
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML = offerId;
}
</script>
<script>
myCreateFunction();
</script>
<?php
$tb->setOfferId();
?>
<script>
myCreateFunction()
</script>
Yes, because myCreateFunction is being rendered by your browser on your second call. PHP (server-side) has nothing in common with JavaScript (client-side). Don't mix your code like that.
Instead, you can use setters, getters, and then pass the value to your JavaScript function as parameter.
You have some problem in understanding where you are in your code (in javascript or in php). $tb->setOfferId(); does not modify anything in your javascript. So the second call to myCreateFunction() will be exactly the same.
I have this code in my HTML code. I'm trying to assign a field to a variable and then show it in my HTML code. What I'm trying to accomplish is showing in my HTML code just the last four digits of the 'ID' data field, right now shows 8 digits. This is my code:
<body onload="myFunction()">
<script>
function myFunction()
{
var str = ID //'ID' IS A DATA FIELD BUT 'STR' VARIABLE DOESN'T TAKE IT**
var res = str.substring(5, 8);
document.getElementById("javi").innerHTML = res;
}
</script>
And then I call it int the html body
<p id="javi"></p>
var str = document.getElementById('Javi').textContent
should get you the text of your p
from there your substring should work
I am new in Javascripting language.
I tried to build an application in which , there is one HTML page from which I get single input entry by using Submit button, and stores in the container(data structure) and dynamically show that list i.e., list of strings, on the same page
means whenever I click submit button, that entry will automatically
append on the existing list on the same page.
HTML FILE :-
<html>
<head>
<script type = "text/javascript" src = "operation_q_2.js"></script>
</head>
<body>
Enter String : <input type= "text" name = "name" id = "name_id"/>
<button type="button" onClick = "addString(this.input)">Submit</button>
</body>
</html>
JAVASCRIPT CODE
var input = [];
function addString(x) {
var s = document.getElementById("name_id").value;//x.name.value;
input.push(input);
var size = input.length;
//alert(size);
printArray(size);
}
function printArray(size){
var div = document.createElement('div');
for (var i = 0 ; i < size; ++i) {
div.innerHTML += input[i] + "<br />";
}
document.body.appendChild(div);
//alert(size);
}
Here it stores the strings in the input Array, but unable to show on the web page. Need help please.
Tell me one more thing there is one code on given link. It also not gives desired answer. Please help me overcome from this problem.
<html>
<body>
<script>
function addValue(a) {
var element1 = document.createElement('tr');
var element2 = document.createElement('td');
var text = document.createTextNode(a);
var table = document.getElementById('t');
element2.appendChild(text);
element1.appendChild(element2);
table.tBodies(0).appendChild(element1);
}
</script>
Name: <input type="text" name="a">
<input type="button" value="Add" onClick='javascript:addValue(a.value)'>
<table id="t" border="1">
<tr><th>Employee Name</th></tr>
</table>
</body>
</html>
In your code where you push an item to the end of your input array, you're trying to push the array instead of the value to the array. So if your problem is that your values aren't being appended to the page is because you're trying to append the array that's empty initially onto itself.
So instead of
input.push(input);
It should be
input.push(s);
Since "s" you already declared to be the value from the text field.
And if you're not going to use that parameter you're passing in, I would get rid of it.
References: Javascript Array.Push()