This question already has answers here:
Why is document.write considered a "bad practice"?
(17 answers)
Closed 1 year ago.
Can You help me find out how to use the html button element in JavaScript because
document.write("<body> <div> </div> </body>")
is not working for buttons or drop down menus so can you help me?
my guess is the
.write
Is the problem because that probably means text and a button is not text.
This is not the most correct way to do this. You must create elements and add them to the DOM tree.
let button = document.createElement('button');
document.body.append(button);
button.onclick = () => {
alert('button clicked');
}
I think you want append(). This example is stolen from this MDN page
let parent = document.createElement("div")
let p = document.createElement("p")
parent.append(p)
console.log(parent.childNodes) // NodeList [ <p> ]
Related
This question already has answers here:
How to get a dom element in a new window?
(3 answers)
access a dom element of a different page in Javascript
(1 answer)
Closed 6 months ago.
I want to appendChild an element to another page after clicking a button.
I used document.getElementById() to select the element I want to appendChild to.
But it throws this error in the console - Cannot read properties of null (reading 'appendChild').
It works when I appendChild paragraph to the same page in which I click the button.
For both html documents I load the script at the bottom.
class Pojistovna {
constructor(){
this.tableOfInsured = document.getElementById("table-of-insured-container")
this.createInsured()
}
createInsured() {
this.continueButton.addEventListener("click", () => {
let paragraph = document.createElement("p")
paragraph.textContent = "Hi"
this.tableOfInsured.appendChild(paragraph)
}
}
}
Move this.tableOfInsured = document.getElementById("table-of-insured-container") to the top of createInsured fun.
This question already has answers here:
Accessing textNode.style in DOM
(2 answers)
Closed 11 months ago.
I'm trying to create a click event for my button to change the text decoration I keep getting this error under the done event listener.
This is my code-
let li = document.createElement('li');
let rmv = document.createElement('button');
let done = document.createElement('button');
rmv.classList.add('remove-button');
done.classList.add('done-button');
li.classList.add('li-item');
rmv.textContent = 'X';
done.textContent = '✔️';
rmv.addEventListener('click', handleRemove);
done.addEventListener('click', function(){
txt.style.textDecoration = "line-through";
})
This will fix it. It's better to utilize relative paths like this for dynamically created output. It gives you greater flexibility. To explain, e is the event of the click. The click listener always has a single argument event, and you can access the element that fired it with event.target. Then you find the closest 'li' container and apply the style to that. You can use the same logic to remove the element as well.
done.addEventListener('click', function(e){
e.target.closest('li').style.textDecoration = "line-through";
})
This question already has answers here:
Dynamically creating HTML elements using Javascript?
(6 answers)
Closed 2 years ago.
Trying to make an Html div inside a div that is already made using javascript but my function has a problem.
Html part
<div id="test">
<button onclick="addDiv()">test</button>
</div>
Js part
let page = document.querySelector('test');
function addDiv() {
document.createElement('div')
document.querySelector('page')
addedDiv = document.appendChild('div')
page = document.appendChild('div')
document.getElementById('div').innerHTML = "ThisFunctionIsWorking"
}
the output should be seen in the console with a text inside the div that says ThisFunctionIsWorking
but instead I get an error(Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.)
I would appreciate your time helping me...
Your code is incorrect in many ways. I suggest you study basic (web) programming with the emphasis on fundamentals like return values, scope, etc. and then basic javascript and dom tree manipulation.
A solution to your problem is:
<div id="test">
<button onclick="addDiv('test')">test</button>
</div>
function addDiv(nodeId) {
var elem = document.createElement('div')
var container = document.getElementById(nodeId)
container.appendChild(elem)
elem.innerHTML = "ThisFunctionIsWorking"
}
This question already has answers here:
How to get the pure text without HTML element using JavaScript?
(10 answers)
Closed 5 years ago.
Is there a method or function that does this, or do I have to check each character of a string. To give an idea on what I'm talking about, for example. I have an HTML TEXT like:
<p><strong>Welcome </strong>to this <em>message, </em><span style="text-decoration: underline;">you may come in peace.</span></p>
I need to convert it into a plain text, resulting into:
Welcome to this message, you may come in peace.
The text will come from a textarea which is a child of a div with an id = editor-email.
I also wanted to take the current text, but it won't work.
var textEmail = $('#editor-email').find('textarea').text();
You can do it like with pure JS
let a = `<p><strong>Welcome </strong>to this <em>message, </em><span style="text-decoration: underline;">you may come in peace.</span></p>
`;
let d = document.createElement('div');
d.innerHTML = a;
console.log(d.innerText);
If you're looking for a JQuery-free solution, you can select the element and use .innerText:
document.querySelector('#myElement').innerText
I think you need:
$("p").text()
EDIT:
If you want the value from the textarea then you will need:
$("#editor-email > textarea").val();
This question already has answers here:
jQuery if div contains this text, replace that part of the text
(5 answers)
Closed 8 years ago.
...
<div>
<div>
{FirstName} {POSITION}
</div>
</div>
...
We would like find element {FirstName} and repalce him on test.
For this we make:
$("{FirstName}").replaceWith("test");
But it is not working...
Tell me please how right make replace?
What you should do is target the parent DIV (lets pretend it has a class of .test)
var content = $('.test').html();
var new_content = content.replace('{Whatever}', 'Hello');
$('.test').html(new_content);
Or in short (I haven't tested this, but it should work)
$('.test').html($('.test').html().replace('{Whatever}', 'Hello'));
If you just want to replace a string in DOM elements, the question has been asked before :
Replace all strings in dom element
But best to do this would be to use a template engine.