6

Is it a good or bad practice to use jQuery statements in plain javascript functions outside of ready? If not what is the correct way?

//** End ready **//

function doSomething() {
    selectedClickBoxIndex = parseInt( jQuery('.' + clickBoxOnClass ).attr('value') );

    // Javascript code  
}
5
  • Yes, of course it is. You can use jQuery functions wherever you want and need. Commented Apr 23, 2012 at 13:24
  • 1
    as far as i've learnt jQuery is an effective shorthand alternative to javascript to introduce unobtrusive scripting.. so using jQuery in conjuction with javascript is not something i'd recommend as it'd feel like defeating the purpose of jQuery, also the modularity of code and uniformity gets affected when multiple people work mixing js and jQuery making it very hard to look at Commented Apr 23, 2012 at 13:27
  • 6
    @optimusprime619 jQuery isn't an alternative to JavaScript - it is JavaScript. It is in fact inherently impossible to use jQuery without using JavaScript. Commented Apr 23, 2012 at 13:33
  • @JamesPoulson note that to get the value of an element you should probably use .val() and not .attr("value") ... Commented Apr 23, 2012 at 13:34
  • @Pointy i used the world alternative in the context of using "implementation" :) but yes, i understand that it is actually javascript.. Commented Apr 23, 2012 at 13:55

7 Answers 7

5

nothing wrong. When you include the jquery file, you're expanding your functionality, free to use wherever you want

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

3 Comments

jQuery doesn't expand functionality. There's nothing with jQuery that you can't do without it.
Agreed. It just makes existing functionality (much) easier to implement.
@ElliotBonneville: Sometimes.
3

There is nothing wrong with that. jQuery is just a Javascript library, there is no strict separation between them.

You might want to use the function val, though:

function doSomething() {
  selectedClickBoxIndex = parseInt( jQuery('.' + clickBoxOnClass ).val() );

  // Javascript code  
}

Comments

2

That's completely acceptable; just make sure you call those functions after the DOM has loaded, either from document ready or some equivalent.

1 Comment

Actually, you can call it before ready, just make sure it's after the specific elements you want the script to handle has been parsed.
2

There's nothing wrong with that at all.

What you want to make sure is that no references to DOM nodes are made before DOMReady. This is true regardless of whether you use $('.someClass') or document.getElementsByClassName.

Your function does make such a reference, but this is fine as long as the function itself is not called before DOMReady.

Comments

2

There is no reason not to use it outside of the document.ready. It makes it much easier to traverse and modify the dom.

The ability to grab elements on the page through the many useful jquery selectors is a good enough reason to use it elsewhere.

Comments

2

As long as function doSomething() is triggered after onLoad event it is totally OK to do it that way. $(document).ready is just a way for JS execution to be postponed until page is fully loaded.

Comments

1

Why not this will reduce the line of code and also jquery is compatible with all the browsers

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.