Skip to content
Advertisement

JavaScript add events cross-browser function implementation: use attachEvent/addEventListener vs inline events

In order to add events we could use this simple first solution: or this second solution (that adds inline events): These are both cross-browsers and can be used in this way: Since I have the feeling attachEvent/addEventListener are used more around in events handling implementations, I’m wondering: Are there any disadvantages/drawbacks against using the second solution that I might better

How to generate sequence of numbers/chars in javascript?

Is there a way to generate sequence of characters or numbers in javascript? For example, I want to create array that contains eight 1s. I can do it with for loop, but wondering whether there is a jQuery library or javascript function that can do it for me? Answer You can make your own re-usable function I suppose, for your

On Text Highlight Event?

I’m curious if anyone knows how I would trigger a function to run if/once the user finishes selecting text on the web page? I would like the user to be able to select text, and after a short delay(or immediately, at this point it doesn’t matter much) an overlay button appears near the text that the user can then click

HTML input fields does not get focus when clicked

I have a problem and I can’t figure out what exactly is causing this behavior. I cannot access my input fields and textareas on my HTML form. Unfortunately, the JS, HTML and CSS are very large, so I can’t really post it all here. Can anybody tell me what to look for when debugging this strange behavior? UPDATE If I

Append Style to DOM not Replacing Existing

How can I append style element to DOM without eliminating existing style on the item (eg color, text-align, etc)? The event calls the function, but the problem is ‘Style’ gets completely replaced with the single item instead. I have simple code triggered on the event: Answer Which browser are you using? In Chrome, this works for me: When I click

JavaScript file upload size validation

Is there any way to check file size before uploading it using JavaScript? Answer Yes, you can use the File API for this. Here’s a complete example (see comments): Slightly off-topic, but: Note that client-side validation is no substitute for server-side validation. Client-side validation is purely to make it possible to provide a nicer user experience. For instance, if you

window.onload vs $(document).ready()

What are the differences between JavaScript’s window.onload and jQuery’s $(document).ready() method? Answer The ready event occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded. The onload event is a standard event in the DOM, while the ready event is specific to jQuery. The purpose of the

Advertisement