Uncaught TypeError: Cannot read property ‘top’ of undefined
Check if the jQuery object contains any element before you try to get its offset:
Check if the jQuery object contains any element before you try to get its offset:
These solutions will work: As mentioned in comments use defer: or or or even Note that the last option is a better way to go since it is unobstrusive and is considered more standard.
You can check whether element.className matches /\bthatClass\b/.\b matches a word break. Or, you can use jQuery’s own implementation: To answer your more general question, you can look at jQuery’s source code on github or at the source for hasClass specifically in this source viewer.
Update April 2019 jQuery isn’t needed for cookie reading/manipulation, so don’t use the original answer below. Go to https://github.com/js-cookie/js-cookie instead, and use the library there that doesn’t depend on jQuery. Basic examples: See the docs on github for details. Before April 2019 (old) See the plugin: You can then do: To delete: Additionally, to set a timeout … Read more
Unlike innerText, though, innerHTML lets you work with HTML rich text and doesn’t automatically encode and decode text. In other words, innerText retrieves and sets the content of the tag as plain text, whereas innerHTML retrieves and sets the content in HTML format.
If it’s about 10px, then I guess you could simply manually adjust the containing div‘s scroll offset like that:
I figured it out! After reading this blog post I realized that the placement of this line: was wrong. That line needs to be the last line in the <body> section, right before the </body> tag. Moving the line down solves the problem. My explanation for this is that react was looking for the id in between the <head> tags, instead of in … Read more
Update: In the past few years the landscape has changed drastically. You can now reliably use querySelector and querySelectorAll, see Wojtek’s answer for how to do this. There’s no need for a jQuery dependency now. If you’re using jQuery, great…if you’re not, you need not rely it on just for selecting elements by attributes anymore. There’s not a very short way … Read more
The reason you’re seeing that error is because document.getElementsByName returns a NodeList of elements. And a NodeList of elements does not have a .value property. Use this instead:
I have heard that querySelector and querySelectorAll are new methods to select DOM elements. How do they compare to the older methods, getElementById and getElementsByClassName in terms of performance and browser support? How does the performance compare to using jQuery’s query selector? Is there a best practice recommendation for which method to use?