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

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);

Related

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

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.

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 call functions dynamically in Javascript [duplicate]

This question already has answers here:
How to pass extra parameter to event handling callback?
(2 answers)
Pass an extra argument to a callback function
(5 answers)
Closed 5 years ago.
I have a function where I would like to call classList methods dynamically, something like this:
function expand(action) {
searchContainer.classList[action]("expanded");
}
button.addEventListener("click", expand('add'), false);
searchContainer.addEventListener("onblur", expand('remove'), false);
But not sure how can I do that?

Result of syntax in JS [duplicate]

This question already has answers here:
How is this valid javascript, and what purpose does it serve?
(2 answers)
Closed 7 years ago.
I'm switching from C++ to JS.
//if ( condition )
{
instruction1;
instruction2;
}
In C/C++ commenting if before the block would simply execute the block unconditionally.
Could I use it the same way in JS? Or would it create an unnamed object which is never used?
A very quick way of testing:
{
alert("foo");
}
http://jsfiddle.net/4obksb0s/
Yes - it appears to run fine.

what is the usage of $("#element").on("click", function(event){ }); in jquery? [duplicate]

This question already has answers here:
What's the difference between `on` and `live` or `bind`?
(7 answers)
Closed 9 years ago.
I am newbie to jquery and I would like to know the difference between a
$("#element").on("click", function(event){ });
and
$("#element").live("click", function(event){ });
function.
Well for one $.live() is deprecated as of 1.7.
Also for more details you can check this answer:
What's the difference between jQuery .live() and .on()

Categories