Can someone please help me with how to push different attributes of elements with the same classname into an array? I'm not that good at Javascript and this is what I have tried but the arrays turns up empty.
One array should contain the URL of images and the other one is for image-texts in a span-element:
function slideShow(){
imgUrlList.length = 0;
imgTextList.length = 0;
var imgUrl = document.getElementsByClassName('markedImg');
var imgText = document.getElementsByClassName('marked');
for(j=0; j<imgUrl.length; j++){
imgUrlList.push(imgUrl.src);
}
for(k=0; k<imgText.length; k++){
imgTextList.push(imgText.innerHTML);
}
openWindow();
}
Related
I have three images, each with their own id. First image's id = "1", second image's id = "2", third one's id = "3".
I have an array with three images. I would like to change the previously mentioned images to the ones within the array. To do this I have made a for loop.
for (var i = 0; i < myArray.length; i++){
var idString = (i).toString();
document.getElementById(idString).src = myArray[i];
}
However, it isn't able to get the id. If I replace idString with "2" then it switches the image with the id = "2" perfectly, so I know for sure that the problem is convering i to string to get the id.
Thank you for reading. Any help will be grealy appreciated.
Your for loop is not starting at 1. Add 1 to i to get the IDs of the images.
for (var i = 0; i < myArray.length; i++){
var idString = (i + 1).toString();
document.getElementById(idString).src = myArray[i];
}
I am trying to create an array of images in javascript for my website when I declare array size it is working but when I am trying to take it as a dynamic array it is not showing images. can anyone tell me what's wrong with this code?
var theImages = new Array()
for (i = 0; i < theImages.length; i++)
{
theImages[i] = i+".jpg"
}
The initial size of theImages is 0.
You need to use
for (i = 0; i < 5; i++)
{
theImages[i] = i+".jpg";
}
replace 5 by the number of images you have.
This is happening because you are looping an empty array, the length/size of the array is 0 because you have just created a new array and are looping it without any elements in it yet.
If you wish to add all images to an array, you will have to know the total number of images you have/ count of images, and then run the loop to add the images in the array, which you were able to do successfully you said.
var theImages = new Array();
for (i = 0; i < 6; i++)
{
theImages[i] = i+".jpg";
}
If you are getting the image names for SQL, it is a different query you want to use, let me know if that is what you are looking for.
Essentially what I'm trying to do right now is, given some input text, I split it up by white space and display on a
div id= "animation"
Every time a button is clicked, the array should go forward one word.
This is my current attempt.
function displayText() {
var displayText = document.getElementbyID("animation");
var list = (document.getElementbyID("input").split(/[ \tn]+/);
for (var i = 0; i < list.length; i++) {
displayText.innerHTML = list.get[i];
}
}
Is my thought process somewhat correct? For whatever reason, it doesn't seem to be working.
there are multiple issues in your method
function displayText() {
var displayTextAnimation = document.getElementbyID("animation"); //keep variable name and method name different
var list = (document.getElementbyID("input").value).split(/[ \tn]+/); //use value property and observe extra bracket
for (var i = 0; i < list.length; i++) {
displayTextAnimation.innerHTML = list.charAt(i); //observe replacing get by charAt
}
}
I have an multi-dimension array that i'm iterating through. Is there a way to put the contents of the array into a new multi-dimension array creating a new MDA? For example, the following code puts all indices of the original array into the new candies array if there's a match. Currently i'm doing
candies.push([product[0],product[1], etc...]);
I'm just trying to see if there's a faster/cleaner way to get that in there.
I tried:
candies.push(product);
but that didn't work. Here's the code i'm currently using
var sel = 'candy';
var candies = [];
for(var i = 1; i < products.length; i++) {
var product = products[i];
for(var j = 0; j < product.length; j++) {
if(sel==product[11]){
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
}
break;
}
}
A cleaner way of writing below line:
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
is:
candies.push(product.slice(0,13)]);
Sample example JSFiddle is here.
Best of luck.
(Mark this as answer if it serves your need.)
I'm trying to access an element located in a cell inside of an array, that's inside of another array.
I've tried several methods but everything returns undefined.
json:
[
{"assignment":"Tom" , "cell":["Tom", "2013-10-06", "Client 3", "Activity", "Scheduled" ]}
]
jquery:
$.getJSON('data/gridData1.json',function(json){
var grid = json;
filterGrid(grid, ele);
});
This code does return an array perfectly fine.
javascript:
function filterGrid(filter, ele){
var types = ['Activity','Alert','Lead','Notification'];
var newTable = [];
var cs1 = $("option:selected", ele).attr("class");
var option = $("select[name='datagrid_filter'] option:selected").text().trim();
if(cs1 == 'type'){
for(var i = 0; i < types.length; i++){
if(types[i]==option){
for(var k = 0; k < filter.length; k++){
if(**filter[0][0][0].value==option**){
newTable.push(filter[k]);
}
}
break;
}
}
}
buildGrid(newTable);
}
Doesn't return anything, including the first element.
Any ideas would be great, that.
Your array has one element, which is an object, so filter[0] gives you that object.
That object has two properties, assignment and cell, so filter[0].assignment gives you "Tom" and filter[0].cell gives you the inner array.
The inner array has filter[0].cell.length items in it, the first of which is filter[0].cell[0], the second of which is filter[0].cell[1], etc.
To iterate over the items in the inner array do this:
for(var k = 0; k < filter[0].cell.length; k++){
if(filter[0].cell[k]==option){
newTable.push(filter[0].cell[k]);
break;
}
}
...but it's kind of clunky repeating filter[0].cell everywhere, so you can add another variable that is a reference to the inner array:
var cell = filter[0].cell;
for(var k = 0; k < cell.length; k++){
if(cell[k]==option){
newTable.push(cell[k]);
break;
}
}
Your code that tried to use filter[0][0][0].value didn't work because you can't access object properties by numeric index except where the actual property name is a number, and in any case you don't want the .value bit on the end.