I have a container and i have cloned it to create 3 more containers. The main reason i used the Clone property was because i didnt want to rewrite the code again and again to make the same containers. Now i want to update different data for the cloned containers. I have managed to pass the heading name for the original container as you can see in the code. Now how do I access my cloned containers and change the data in them. I have put the code which i tried below (failed). When i put data[1],data[0] etc, nothing gets updated.
Current JavaScript
$(document).ready(function() {
var e = $('.column'); //column is the class which contains the container
for (var i = 0; i < 3; i++) {
e.clone().insertAfter(e);
}
document.getElementById("heading").innerHTML = ("TRAILER 22");
Failed Javascript , this does not update anything on the webpage.
$(document).ready(function() {
var e = $('.column');
for (var i = 0; i < 3; i++) {
e.clone().insertAfter(e);
}
var data = document.querySelectorAll(".column");
data[1].document.getElementById("heading").innerHTML = ("TRUCK 22");
});
You try to do a document call on a element, that is not possible. If you want to change the first column (i switched the code to jQuery, what makes it a lot easier):
$(document).ready(function() {
var e = $('.column');
for (var i = 0; i < 3; i++) {
e.clone().insertAfter(e);
}
var data = $(document).find(".column:first-of-type");
data.html("TRUCK 22");
});
Related
I have maps of districts named mc_1, mc_2, mc_3 etc...
I'm triggering an event when mouse hovers over them. Instead of writing code for each mc_n I thought I could use arrays. But I havent done that before.
Currently I have
var district = [];
for (var i = 0; i <= 20; ++i) {
district[i] = "mc_" + i;
}
stage.enableMouseOver(3);
this.district[i].on('mouseover', function(){
this.district[i].gotoAndPlay(2);
});
After the for loop, there is no more i variable, instead you can use forEach:
var district = [];
for (var i = 0; i <= 20; ++i) {
district[i] = document.getElementById("mc_" + i); // get the DOM elements by ID
}
stage.enableMouseOver(3);
this.district.forEach( (el) => { // loop through the DOM elements
el.on('mouseover', function(){ // add the muoseover event to the current element
el.gotoAndPlay(2);
});
});
But if you want to apply the same behavior to all of those elements, i would suggest to use CSS classes in order to identify them.
Hello i want to use "var" on multiple Id´s.
// Get modal element
var modal = document.getElementById('modal1');
// Get open modal button
var modalBtn = document.getElementById('Info1');
// Get close button
var closeBtn = document.getElementById('closeBtn1');
This is the code that works, but i want to do is that it is "modal1, modal2 ... and Info1, Info2...
Can you help me with that?
Hey Celtor you can find basic knowledge about JavaScript Variables
in here.
then to do so you can use for loop with array as so:
// Create list of modals with 15 undefined elements
var modals = Array(15);
for (i = 0; i < x.length; i++) {
// Assign the elemets to the list in their index
modals[i] = document.getElementById('modal'+i);
}
Hope this helps!
You will have to use a function like this:
var getElementsByIds = function(ids){
var elements = [];
for(var i = 0; i < ids.length; i++){
elements[ids[i]] = document.getElementById(ids[i]);
}
return elements;
}
This will return an array in which each key is an element you specified, and the key is the element's ID.
I have a listview div on screen and I have added itemDataSource to it successfully
var lettersList = new WinJS.Binding.List(jsonArrayForClearance);
var list_ = document.getElementById("prodListView").winControl;
list_.itemDataSource = lettersList.dataSource;
list_.itemTemplate = document.getElementById("tileTemplate");
list_.forceLayout();
Now in each item I have added a custom input type for user to specify(using template). I want to iterate through all the items of list and obtain the value of that input type in an array.
how can I do it?
EDIT: My question is to access custom input type declared in list items. As such we use following code to access any input type named "inpT"
document.getElementById('inpT');
but how to access the same from list item? can I use Following code(as suggested by user2608614)
var listView = document.getElementById("prodListView").winControl;
var list = listView.itemDataSource.list;
for (var i = 0; i < list.length; i++) {
var item = list.getAt(i);
item.getElementById('inpT');
}
You can iterate through the list elements like this:
var listView = document.getElementById("prodListView").winControl;
listView.itemDataSource.getCount()
.done(function(count) {
for (var i = 0; i < count ; i++) {
listView.itemDataSource.itemFromIndex(i)
.done(function (item) {
//***item will contain your property
});
}
});
Is not the best solution as it make it difficult to chain the promises, I'm also looking for a better one. But it works.
Since you're using a Binding.List you can just iterate through that much like an array.
var listView = document.getElementById("prodListView").winControl;
var list = listView.itemDataSource.list;
for (var i = 0; i < list.length; i++) {
var item = list.getAt(i);
// do something with this item
}
The only thing to remember is that it doesn't support [] and instead you have to use .getAt() and .setAt().
I'm trying to load X amount of <li>'s into a <ul> via a for loop in a jquery function, and while I think I've got the syntax about right I'm not getting anything loading. (no problem with loading a single <li>, but none for multiples with the method I've tried)
Initially I attempted to pass a variable into the loop to determine the amount of increments: var peekListAmount = 5;
That didn't work so I went for a bog-standard loop incrementer. That doesn't work either so, after searching here and getting close, I have put together a fiddle to see if someone can point out what I'm doing wrong: http://jsfiddle.net/janowicz/hEjxP/8/
Ultimately I want to use Knockout.js to dynamically input a number to pass to the loop amount variable, but 1st things 1st.
Many thanks in advance.
When you do:
var peekListItem = $('<li>...</li>');
you're creating a single instance of an <li> node, encapsulated in a jQuery object.
Appending an already-present node to the DOM just removes it from its current place in the DOM tree, and moves it to the new place.
You need to create the node inside the loop, not outside, otherwise you're just re-appending the same node each time, not a copy of that node.
In fact, given you're not manipulating that node, you can just put the required HTML directly inside the .append() call without wrapping it in $(...) at all:
$(function() {
var peekList = $('<ul class="peekaboo-list">').appendTo('div.peekaboo-wrap');
function addLiAnchorNodes(nodeAmount) {
var html = '<li>' +
'<p class="peekaboo-text"></p></li>';
for (var i = 0; i < nodeAmount; ++i) {
peekList.append(html);
}
}
addLiAnchorNodes(5);
});
See http://jsfiddle.net/alnitak/8xvbY/
Here is you updated code
$(function(){
var peekList = $('<ul class="peekaboo-list"></ul>');
var peekListItem = '<li><p class="peekaboo-text"></p></li>';
//var peekListAmount = 5;
var tmp = '';
var addLiAnchorNodes = function (nodeAmount){
//var nodeAmount = peekListAmount;
for (var i = 0; i < 10; i++){
tmp += peekListItem;
}
peekList.append(tmp);
$('div.peekaboo-wrap').append(peekList); // This bit works fine
}
addLiAnchorNodes();
});
This should work. Instead of appending the list item in each loop, append the list only once at the end.
$(function(){
var peekList = $('<ul class="peekaboo-list"></ul>');
peekList.appendTo('div.peekaboo-wrap');
var addLiAnchorNodes = function (nodeAmount){
var list = "";
for (var i = 0; i < 10; i++){
list += '<li>Sample<p class="peekaboo-text"></p></li>';
}
peekList.append(list);
}
addLiAnchorNodes();
});
Here is the updated fiddle
Try this:
$(function(){
var peekList = $('<ul class="peekaboo-list"></ul>');
$(peekList).appendTo('div.peekaboo-wrap'); // This bit works fine
var addLiAnchorNodes = function (nodeAmount){
//var nodeAmount = peekListAmount;
for (var i = 0; i < 10; i++){
var peekListItem = $('<li><p class="peekaboo-text"></p></li>');
peekListItem.appendTo(peekList);
}
}
addLiAnchorNodes();
});
I know that I can run a line of javascript code after the page loads on an iPad with UIWebView (which is what I am using), but I do not know what I could type to go through and remove all of the tags. I also would like to be able to do this to only certain parts of the page e.g. only remove tags within a certain tag.
You can get all elements by tag name using document.getElementsByTagName(). All links have the tag name a. You can visually remove them by setting their display style to none.
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
To remove elements of a certain tag within a certain tag, just invoke getElementsByTagName() on the element in question. Suppose that you want to hide all links inside a <li> only:
var listitems = document.getElementsByTagName('li');
for (var i = 0; i < listitems.length; i++) {
var anchors = listitems[i].getElementsByTagName('a');
for (var j = 0; j < anchors.length; j++) {
anchors[j].style.display = 'none';
}
}
The element.parentNode.removeChild(element) is also a good one, but it doesn't work nicely inside a standard for loop. You need to loop backwards:
var elements = document.getElementsByTagName('a');
for (var i = elements.length; i-- > 0;) {
var element = elements[i];
element.parentNode.removeChild(element);
}
Update as per the clarified functional requirement: you thus want to replace the link element with a text node representing the link's original content? You can use Node.replaceChild() for this. Here's a kickoff example:
var elements = document.getElementsByTagName('a');
for (var i = elements.length; i-- > 0;) {
var element = elements[i];
var text = document.createTextNode(element.firstChild.nodeValue);
element.parentNode.replaceChild(text, element);
}
Thought I'd post a remove() function to complement BalusC:
function remove(el){
if(el.parentNode)el.parentNode.removeChild(el);
}
Note: If the element doesn't have a parent, it means it is not in the DOM tree.
It also means it will be removed in the GC's (garbage collector) next run (as long as there are no references to it).
If you're going to be doing a lot of dom manipulation, it might be worth it to include jQuery to grab your elements. It'd be a little easier to remove items. Eg.
$(function(){
$('.surrounding_class a').remove();
});
If you want to remove links but preserve their display contents (text, images, etc.) you can insert their childNodes before the links, then remove the link elements:
var removeLinks = function(context) {
var undefined;
if(context === undefined) {
context = document;
}
if(!context) {
return false;
}
var links = context.getElementsByTagName('a'), i, link, children, j, parent;
for(i = 0; i < links.length; i++) {
link = links[i];
parent = link.parentNode;
if(!link.href) {
continue;
}
children = link.childNodes;
for(j = 0; j < children.length; j++) {
parent.insertBefore(children[j], link);
}
parent.removeChild(link);
}
return context;
};
// Use:
removeLinks(document.getElementById('container'));