Use document.getElementById when DOM has not rendered [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Vanilla JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it [duplicate]
(10 answers)
window.onload vs document.onload
(9 answers)
Closed 2 years ago.
I have the following jQuery code in a project and I want to remove jQuery library from the project. So I am trying to convert this in to full javascript.
$(document).ready(function () {
$("#price-button").click(priceButtonClicked),
getURLParameter("run") && $("#price-button").click()
});
var priceCell = $("#priceCell");
I tried this using document.getElementById but the issue is my page has not rendered and therefore the elements are not in present at that stage. I have no idea how to overcome this and any help would be greatly appreciated.

Related

Add eventlistener to a class? [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Want to add "addEventListener" on multiple elements with same class [duplicate]
(3 answers)
Closed 1 year ago.
I'm trying to add a eventlistener to a class but it's not working at all, I have done the same method with ID's and it worked perfectly but it's not working for classes. I'm still kinda new to Javascript so I apologize if it's something very simple.
const boxes = document.getElementsByClassName('box_img');
boxes.addEventListener("click", function(){
console.log('Hi');
});

jQuery global variable, not working [duplicate]

This question already has answers here:
What does $(function() {} ); do?
(6 answers)
Closed 4 years ago.
Why is this simple global variable not working inside a function
var test_1 = "None";
$(function(){
console.log(test_1)
})
I am getting the result Undefined
Check, whether you have jquery or not. I have created a jsfiddle (https://jsfiddle.net/2kudvsr0/) for your code and its printing None (desired output).
Just make sure you are executing your code once jQuery is imported successfully.

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

Getting 'cant read if of null' [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
Here is html:
<div class="nama" id="xxx"></div>
And javascript :
var div = document.getElementById('xxx');
alert(div.id)
Why i am getting mistake?Looks like fine for me
The element was not found.
Most probably because this code was ran before that part of the DOM was ready.
Try placing your code below where the HTML is defined.
window.onload = function() {
var div = document.getElementById('xxx');
alert(div.id)
};

What is equivalent to "jQuery.on" in plain JavaScript? [duplicate]

This question already has answers here:
JavaScript event registering without using jQuery
(6 answers)
Closed 8 years ago.
What is the equivalent of the following code in javascript
$("body").on("PhantomInitEvent", function(){
getReviewData();
supplies_onFocus();
});
$("body").trigger("PhantomInitEvent");
It's already in JavaScript. If, however, you mean what it would look like without jQuery:
document.body.addEventListener("PhantomInitEvent", function() {
getReviewData();
supplies_onFocus();
});
element.attachEvent('onPhantomInitEvent', handler);
element.addEventListener('PhantomInitEvent', handler, false);

Categories