All right, say I have this: What would the selector look like if I wanted to get “Option B” when I have the value ‘2’? Please note that this is not asking how to get the selected text value, but just any one of them, whether selected or not, depending on the value attribute. I tried: B…
How can I change an element’s class with JavaScript?
How can I change the class of an HTML element in response to an onclick or any other events using JavaScript? Answer Modern HTML5 Techniques for changing classes Modern browsers have added classList which provides methods to make it easier to manipulate classes without needing a library: Unfortunately, these …
Converting punycode with dash character to Unicode
I need to convert the punycode NIATO-OTABD to nñiñatoñ. I found a text converter in JavaScript the other day, but the punycode conversion doesn’t work if there’s a dash in the middle. Any suggestion to fix the “dash” issue? Answer I took the time to create the punycode below. It it bas…
Reloading a page via AJAX when window.location=self.location doesn’t work
On my homepage I got: Via MooTools, I get these anchor elements by id so that once they’re clicked, a flashy div will popup below them that contains the login or signup form (with methods to stop the propagation of events of course) and upon filling-up the fields the AJAX call kicks in – that̵…
How to change the href attribute for a hyperlink using jQuery
How can you change the href attribute (link target) for a hyperlink using jQuery? Answer Using will modify the href of all hyperlinks to point to Google. You probably want a somewhat more refined selector though. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. “anchor&…
How can I check if a string is a valid number?
I’m hoping there’s something in the same conceptual space as the old VB6 IsNumeric() function? Answer 2nd October 2020: note that many bare-bones approaches are fraught with subtle bugs (eg. whitespace, implicit partial parsing, radix, coercion of arrays etc.) that many of the answers here fail to…
How can I merge properties of two JavaScript objects dynamically?
I need to be able to merge two (very simple) JavaScript objects at runtime. For example I’d like to: Is there a built in way to do this? I do not need recursion, and I do not need to merge functions, just methods on flat objects. Answer ECMAScript 2018 Standard Method You would use object spread: merged…
What is the best way to remove a table row with jQuery?
What is the best method for removing a table row with jQuery? Answer You’re right: This works fine if your row has an id, such as: If you don’t have an id, you can use any of jQuery’s plethora of selectors.
What is the best way to add options to a select from a JavaScript object with jQuery?
What is the best method for adding options to a <select> from a JavaScript object using jQuery? I’m looking for something that I don’t need a plugin to do, but I would also be interested in the plugins that are out there. This is what I did: A clean/simple solution: This is a cleaned up and …
When should I use Inline vs. External Javascript?
I would like to know when I should include external scripts or write them inline with the html code, in terms of performance and ease of maintenance. What is the general practice for this? Real-world-scenario – I have several html pages that need client-side form validation. For this I use a jQuery plug…