JS - document.createElement does not work [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have the following code:
function icon(link) {
var iccon = document.createElement('div');
var iccons = document.createElement('td');
iccon.setAttribute('id', 'icon');
icons.appendChild(iccons);
iccon.setAttribute('onclick', 'window.open("' + link + '");');
iccons.appendChild(iccon);
var icons = document.getElementById('icons');
};
The HTML code is here.

Even though it's hard to say what you're trying to achieve with the code, I must say that it's an ordering error: you want to append the newly created items to the #icons element, which you declare at the very end.
Move the last statement to the top of the function and everything will work as expected:
function icon(link) {
var icons = document.getElementById('icons');
var iccon = document.createElement('div');
var iccons = document.createElement('td');
iccon.setAttribute('id', 'icon');
icons.appendChild(iccons);
iccon.setAttribute('onclick', 'window.open("' + link + '");');
iccons.appendChild(iccon);
};
Here's the updated demo: http://codepen.io/gion/pen/NRmgPJ
I must say that you should be more careful about naming. Things get hard to read when they aren't named well.

Related

How do I use concatenated string in a DIV? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I think my concatenating is fine, but my use of it isn't working. Any help would be appreciated. Thanks.
<script>
var str1 = "/images/";
var str2 = "test.jpg";
var res = str1.concat(str2);
</script>
<div class="product-sprite" data-image="res"></div>
DOM element attributes are not evaluated as variables. You need to assign it to the element property.
var res = str1 + str2; // no need to use .concat()
document.querySelector(".product-sprite").dataset.image = res;

Javascript cant append elements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am getting some data from my server and I want to create element but this code is not working please help me
var filelist=['dummy1','dummy2','etc']
filelist.forEach(file=>{
var newli=document.createElement("li")
var newa=document.createElement("a")
newa.innerHTML=file
newa.setAttribute('href',file)
newli.append(newa)
box.append(newli)
})
Note the box is a div in above code
Here you go, this is working. One needs to assure that before the execution of any DOM accessing functionality, the former has to be ready/loaded.
const fileNameList = ['dummy1', 'dummy2', 'etc'];
const box = document.getElementById('box');
fileNameList.forEach(fileName => {
const newLi = document.createElement("li");
const newA = document.createElement("a");
newA.innerHTML = fileName;
newA.setAttribute('href', fileName);
newLi.append(newA);
box.appendChild(newLi);
});
<div id='box'>Your box</div>

Remove category from phone number in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a phone number in iOS displayed in a list like :
home: (789)654-1281
I want to remove the home or any other category that will attach with the mobile number.I tried replace() in javascript,but it is not working.Please help.
I am developing hybrid application using kony.
Maybe you can try with something like this :
var number = 'home: (789)654-1281';
number = number.split(': ')[1]; // (789)654-1281
Hope it helps.
var input = "home: (789)654-1281"
var position = input.indexOf(': '); // with space
if (position > 0) {
var output = input.substring(position+2); // + 2 is length of ': '
}

jQuery count amount of divs on a page and hide? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm displaying an amount of divs on a page that can be anywhere from 1-50, all will be generated and loaded into the HTML via PHP, but I want to only display 9 initially and then load an additional 9 on a button click until all are loaded.
var alldivs = $('.preview-container').hide();
$('button').on('click', function(){
var turn = alldivs.splice(0, 9);
if (turn.length) {
console.log(turn);
turn.fadeIn();
}
});
Your question is very vague. For a better reference, you need to post your current code, what precisely you need to do and what you have searched so far. So it can help you to receive a better answer. But most likely you are looking for something like this:
$('li').click(function() {
var which = $(this).index();
$('div').find('div').hide().eq(which).show();
});
This is the shortest code I can think to do this:
var alldivs = $('div'); // select the elements you want to show here
$('button').on('click', function(){
var turn = alldivs.splice(0, 9);
if (turn.length) {
turn.fadeIn();
}
});
As the jQuery-selector returns an array with the matched elements you can combine that with the Array splice method to do what you want.
Basically alldivs.splice(0, 9) remove nine items starting at position zero from alldivs and returns the removed items.
Hope it helps.

Abusing JavaScript string operations [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I've got this function in javascript:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
};
It's used like this:
g.changeImage(2);
And it changes image h. The problem is, that it cannot use image from other directory than current.
I'm writing "addon" for the website which should hook into the existing script and change few things. One of them require changing h.src. Is there any way I can fool the browser and change the address of h to custom url using only the function given?
I can't access h directly, nor change existing script on the website. I can only use function given.
Thank you for any help.
One option that I can come up with off the top of my head would be to overwrite the function:
Old Function:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
alert(h.src);
};
g.changeImage("test.png")
New Function:
g.changeImage = function (a) {
h.src = "test - new url " + a + ".png";
alert(h.src);
}
g.changeImage("test")
Here's a quick example for you to play with and see if this will work for what you need: http://jsfiddle.net/2BsQP/

Categories