2

I want to use the Javascript selector document.querySelector insted of $ or jQuery jQuery selector but I want to combine the Javascript selector with jQuery functions (like .getJSON(), .html(), .append() , etc.).

For example:

$.getJSON("list.json", function(data) {
    document.querySelector("#content").html(data.name);
});

Here when I use the document.querySelector I get Uncaught TypeError: undefined is not a function and when I use $ I don't get any error.

jsFiddle Test

Is it possible to run jQuery and Javascript together?

Thanks!

12
  • 2
    why you want to use both jquery has all options Commented May 25, 2014 at 18:04
  • 4
    You can do $(document.querySelector("#content")).test(data.name), but it makes no sense. And won't work in browsers that don't support querySelector. Commented May 25, 2014 at 18:07
  • 3
    Of course you can mix both syntaxes - JS and jQuery. Sometimes pure JS is faster, e.g. using this.id is faster than $(this).attr('id'). Keep in mind that when mixing, code might get confusing. Either way, it's your personal choice. Commented May 25, 2014 at 18:09
  • 2
    @dandavis Erm, jQuery is JavaScript, so that comment makes no sense... Commented May 25, 2014 at 18:18
  • 2
    @wumm: it works in ie7, which runs jScript, not javascript. there are only a few hundred differences between jScript and JavaScript, but they are not the same... Commented May 25, 2014 at 18:18

3 Answers 3

5

Off couse yes! It is possible to run jQuery and JavaScript together in you application.

All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes. But, yes! You can work with them together.

Secondly, remember that jQuery is a JavaScript's Library. It isn't anything other than JS. To be simple, jQuery needs JavaScript to run. JavaScript doesn't need jQuery to run.

From this MDN source, it is stated that you can use that method just the way it is.

document.querySelector(".someclass");

https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector

All you now need to make sure is of that, that class you're trying to access exists.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! But the document.querySelector doesn't work with jQuery functions. Please see this test: jsfiddle.net/g4ZZK
@Fuiba That's because many of jQuery's methods are properties on a jQuery object, and the way to get a jQuery object is to pass something to the $/jQuery function. A jQuery object is not an Element object, which is what is returned by querySelector(). This is why Bergi suggested what he did in his comment (the fourth comment on your question that he mentions here), because that wraps the Element from querySelector in a jQuery object, thereby allowing you to now use many of jQuery's methods.
2
$.getJSON("list.json", function(data) {
    $(document.querySelector("#content")).html(data.name);
});

PS:

But there isn't any sense to use it everywhere. Check the @afzaal-ahmad-zeeshan answer & read how to use native functional of DOM elements. jQuery isn't a panacea.

Comments

0

jslayer's answer gave me an idea, which seems to work. Wrapping js code in $() seems to work (though I'm not sure why).

For example, to use slideToggle() (which is only available in jQuery, I think), doing

event.target.nextElementSibling.slideToggle()

does not work, but

$(event.target.nextElementSibling).slideToggle()

does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.