Mixing javascript and jquery [duplicate] - javascript

This question already has answers here:
What's the difference between '$(this)' and 'this'?
(7 answers)
Closed 6 years ago.
$(this).append('hello');
The above could be a line nested in some function of jquery. I know that this is a correct line, but I was wondering whether you could mix javascript with jquery and write it down like this.
this.append('hello');
The only difference is that in the second line of code the this keyword is not written inside the jquery method, but this should still work because the this keyword itself automatically stores the location of the node at which the function is aimed. Right? Well apparentely it does not work, so when and why do can't you use the this keyword alone?

The only difference as far as I understand is the wrapper, which is the jQuery object: https://learn.jquery.com/using-jquery-core/jquery-object/. This wrapper exposes jQuery methods on the element, whereas just using this returns the element, but does not expose those same methods. As a simple example, this will return the textContent of the div,
$('div').click(function() {
console.log($(this).text());
})
Where as this will not
$('div').click(function() {
console.log(this.text());
})

Related

Javascript onMouseMove event syntax [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 3 years ago.
I am working on some JavaScript code and I run into this syntax that I have never seen and I am trying hard to understand but cannot find good examples.
Can someone please describe what might be going on here?
function onMouseMove(event) {
(function(ev) {
// some piece of code
})(event);
}
This syntax is used to create an inner scope using a function and that function is immediately invoked with the event object.

Why can't we use setAtrribute function in jquery? [duplicate]

This question already has answers here:
What does jquery $ actually return?
(9 answers)
Closed 6 years ago.
I have tried to use setAttribute() method in jquery as mentioned below :
$(p).setAttribute('class','first');
However that didn't work so I replaced setAttribute with attr() method.
$(p).attr('class','first');
It worked when I used the above code.
I just want to know why it didn't work when I used setAttribute() and why it worked when I used attr()?
.setAttribute() is a function in javascript works on DOM element where .attr() is a jQuery function works on jQuery object (DOM Object)
you can check it by doing
$(p)[0].setAttribute('class','first');
OR
p.setAttribute('class','first'); // no need to wrap in $

How to use "this" keyword [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 8 years ago.
im fairly newb when it comes to writing pure javascript, but i want to learn and become better.
i have a code below, what im trying to do in the if block doesnt matter so much, but what is tripping me up is using "this". below i want wrap to have classname if condition is true, but its not working.
Can anyone please explain why is "this" not working in my code below?
var wrap = document.getElementById("wrap")
if(wrap.innerHTML.length === 0){
this.className="empty"
}
Try wrap.className = "empty".
this refers to the current "execution context" (which you can get a great explanation of in this previous answer). The fact that you're within an if statement doesn't change the execution context or anything... if you're just trying to add that className to the wrap element like you suggest, then apply the className to wrap not this.

Why can't one set an alias to document.getElementById()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function aliasing doesn't seem to work
Set document.getElementById to variable
Code would be more efficient if this was possible:
var min = document.getElementById;
and then call document.getElementById() using min().
Not trying to write minified code but in this particular case one can reduce scope lookup and shorten some lines.
Is this a syntax issue or a limiation on the language?
When you call foo.bar() then this is set to foo inside bar
When you copy foo.bar to window.bar then call window.bar(), this is set to window.
getElementById has to operate on a DOM document object.

What is meaning of "(function(window, undefined){})(window)" in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
(function(window, undefined){})(window);
What is the meaning of this code? I have seen it in many documents, especially in jQuery documents.
How does this work, and why is it defined thus?
You are scoping a piece of code..
By.
Defining it within an anonymous function //function(){...}
Executing it. //(function{})(args)
Also, passing the window parameter allows for faster resolution of the meaning of that variable within your block of code.

Categories

Resources