1

I know basics of both jQuery and JavaScript.I would like to follow a good coding practice. Is using JavaScript inside a jQuery function is a bad practice?

For exapmle:

$(document).ready( function() {
    $('#dqualification').change(function() {
        document.getElementById("demo").innerHTML = "Resolve following errors"; 
    });
});
4
  • 5
    yes it is bad practice, never mix the two languages under threat of hellfire and damnation ... jQuery is written in a programming language called javascript ... so, take the opening sentence as sarcasm Commented Jul 29, 2016 at 6:37
  • @JordiCastilla : Thanks for the clarification .So I can mix both freely .Don't worry I wont do that until it is necessary :) Commented Jul 29, 2016 at 6:50
  • 1
    if you do mix them, make sure you are wearing appropriate hazmat suit - level 4 at least ... /sarcasm Commented Jul 29, 2016 at 6:51
  • 1
    jQuery is a Javascript Library. For performance sometimes you coud need use javascript directly. Commented Jul 29, 2016 at 6:58

2 Answers 2

3

Nope, its fine, and it's done all the time.

Consider:

$("#myDiv").on("click", function(){
    var a = this;
});

The line var a = this; is pure JavaScript. There is no "jQuery Version" of var a = this;.

jQuery provides convenient ways of doing things in JavaScript that might be difficult to write and code yourself in pure JavaScript. It doesn't replace JavaScript, it just 'adds to it'.

Remember, jQuery is just a JavaScript library. So everything ends up as JavaScript at the end of the day.

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

2 Comments

I think this is the best answer :)
But, but, what about var $a = $(this)? :-P
2

It is not directly bad practice, it is more a bit inconsistend. Both is javascript. But why would you do things like in your example? If you have jQuery available, you could use it!

$(function() {
    $('#dqualification').change(function() {
        $("#demo").html("Resolve following errors");
    });
});

10 Comments

Yes , If you have jQuery available, I am using that only .This one I gave for exmple . But in some situations I supposed to use javascript. Is that a bad practice ?
jQuery is javascript, did you not get that?
Even if jQuery is available you should use pure Javascript over jQuery, because it is faster.
@Sree - mixing javascript and jquery is like mixing water at 20 degrees and water at 21 degrees ... it's all water ... perhaps you mean directly using DOM methods vs the jQuery wrappers on DOM methods?
@Sree, if someone told you it is bad practice, then they should give you examples why it is! It's a bit unconventional, to write it like in your entering example, but it's not forbidden nor wrong!
|

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.