Javascript cant append elements [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 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>

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;

Add 2 variables inside another variable [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 trying to create a search variable taking the value of 2 others variables for search results
But I don't manage to get the expected result (title + author). In a sense where the author value is not taking into consideration.
How can I write the following correctly the get both correct values (title + author) in the search variable?
Thank you
let title = document.getElementById('titre-livre').value;
let author = document.getElementById('auteur').value;
let search = title + author;
Remember to separate these values with a space:
let search = [title, author].join(' ');

html input value is not getting fetched 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 2 years ago.
Improve this question
I wrote a simple code to fetch a value from text field and so its summation and display it in the third text field
Correct code
var a = document.getElementById("fNo");
var b = document.getElementById("sNo");
var c = document.getElementById("tNo");
var d = document.getElementById("abc");
d.addEventListener("click", function() {
c.value = Number(a.value) + Number(b.value);
});
But when I obtain value in a like this
a = document.getElementById("fNo").value;
It doesnt work.
You will get a string querying the value.
If the input element is of type "number" you can use:
a = document.getElementById('fNo').valueAsNumber;
otherwise you have to convert the string to a number, before you can do the calculation:
a = Number(document.getElementById('fNo').value);
b = // etc.

how to receive GET parameter from current page with js [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 need to receive get parameter from the current page to do xhr request further. I've tried to set data- tags but I think this kludge is not a good deсition.
Give this a try
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

JS - document.createElement does not work [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 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.

Categories