I'm trying to insert a DOM element using jQuery but it keeps being inserted as text instead.
var newImg = "<h1>Hi</h1>";
$("li").each(function myFn(){
this.addEventListener("mouseover", function myFn2 () {
this.before(newImg);
})
});
If you console.log(this)
you should find that it is not a jQuery object inside the event handler. You will want to wrap it with $() before you use the before method to use the jQuery method.
$(this).before(newImg);
var newImg = "<h1>Hi</h1>";
$("li").each(function myFn() {
this.addEventListener("mouseover", function myFn2() {
$(this).before(newImg);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>Wee</li>
</ul>
How to kill a query running by pd.read_sql and connected by sqlalchemy (or mysql.connector)
Radio and checkbox cannot be checked when inside BootStrap Modal
I am creating a mobile-first site using the Bootstrap 4 frameworkI am trying to create a JavaScript function which will change where a <a> tag is pointing to based upon the screen size of the browser
I use a standard slider slider, and I want to add an event outside the slider itself but on the same pageI tried to catch the event in different ways: