This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 5 years ago.
I have recently asked a question about this. But after starting a new project, I cannot figure out how to display a number of businesses in the system. There are 0 businesses. How do I make the p tag show it up?
<script>var businessesfound = 0; document.getElementById('businessamount').innerHTML = "Business amount:" + businessesfound;</script>
Please help! Thanks for all the support you have given me on other questions :)
The script is executed before the Tag is defined, so the JavaScript just canĀ“t find it. Have a look in the F12-Tools / Console!
Make sure the script tag is added after the div tag OR run the script after the DOM is ready, like this:
$( document ).ready(function() {
var businessesfound = 0;
document.getElementById('businessamount').innerHTML = "Business amount:" + businessesfound;
});
var businessesfound = 0; document.getElementById('businessamount').innerHTML = "Business amount:" + businessesfound;
<p id="businessamount"></p>
Related
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> ]
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:
jQuery match part of class with hasClass
(6 answers)
jQuery selector for id starts with specific text [duplicate]
(4 answers)
Closed 4 years ago.
Trying to get some help on some JavaScript we are trying to post accross multiple landing pages. We are sending values to hidden fields on a button click 'lp-pom-button-3'.
However, the ID changes depending on the page, but will always have the format 'lp-pom-button-' with the number potentially changing depending on the page. Is there a function similar to 'like' in SQL, or saying any button that starts with or contains 'lp-pom-button-" for this to be triggered on? Thanks for you help!
<script type="text/javascript">
document.getElementById('lp-pom-button-3').addEventListener(
'click', function(event) {
ga(function() {
var tracker = ga.getAll()[0];
var clientId = tracker.get('clientId');
document.getElementById('GACLIENTID').value = clientId;
var userId = tracker.get('userId');
document.getElementById('GAUSERID').value = userId;
});
});
You can use the css selector for attribute value contains.
[id*="lp-pom-button-"]
or attribute value starts with:
[id^="lp-pom-button-"]
In vanilla javascript you can access elements using css selector with querySelector or for getting a list of elements querySelectorAll.
Here is an example:
document.querySelector('[id^="lp-pom-button-"]');
And HERE is a list of all the browsers and browsers version that support this function.
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:
Can I create script tag by jQuery?
(5 answers)
Closed 8 years ago.
So i'm trying to put a <script> tag with some js code inside a div, well this is the code that i'm using:
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "var dfpValues = DFP.get().values(); googletag.display(dfpValues.dfpContainer)"
$("#mydiv").html(script);
This code puts nothing inside my div, and returns no errors.
Someone know how to do?
Obs: im using ruby(v2) on rails(v4) project, this can change something?
I keep the script to be added in a string, then append the string TO the div (in case there are things in the div, these wont get removed).
var scriptString = "<script type='text/javascript'> var dfpValues = DFP.get().values(); googletag.display(dfpValues.dfpContainer) </script>";
$(scriptString).appendTo('#myDiv');
$("#mydiv").html("<script>var dfpValues = DFP.get().values();googletag.display(dfpValues.dfpContainer)</script>");