.js.erb VS .js

.js.erb files are for controller actions, such as create, when you want javascript to be executed when the action completes. For example, when you want to submit a form via ajax and want display an alert when everything is done, you would put the $(‘#business_submit’).click(…) in your application.js or *.html.erb, and your create.js.erb could look … Read more

How do I style radio buttons with images – laughing smiley for good, sad smiley for bad?

Let’s keep them simple, shall we. First off, using pure HTML + CSS: <div id=”emotion”> <input type=”radio” name=”emotion” id=”sad” /> <label for=”sad”><img src=”https://stackoverflow.com/questions/3896156/sad_image.png” alt=”I’m sad” /></label> <input type=”radio” name=”emotion” id=”happy” /> <label for=”happy”><img src=”happy_image.png” alt=”I’m happy” /></label> </div> This will degrade nicely if there’s no JavaScript. Use id and for attributes to link up the … Read more

form action with javascript

Absolutely valid. <form action=”javascript:alert(‘Hello there, I am being submitted’);”> <button type=”submit”> Let’s do it </button> </form> <!– Tested in Firefox, Chrome, Edge and Safari –> So for a short answer: yes, this is an option, and a nice one. It says “when submitted, please don’t go anywhere, just run this script” – quite to the … Read more

How do I select the parent form based on which submit button is clicked?

You can select the form like this: $(“#submit”).click(function(){ var form = $(this).parents(‘form:first’); … }); However, it is generally better to attach the event to the submit event of the form itself, as it will trigger even when submitting by pressing the enter key from one of the fields: $(‘form#myform1’).submit(function(e){ e.preventDefault(); //Prevent the normal submission action … Read more

How can I void a form action and execute jQuery when all HTML form elements are validated?

According to this: Do any browsers yet support HTML5’s checkValidity() method?, and this may not be the latest truth since HTML5 is a work in progress, the Form.checkValidity() and element.validity.valid should let you access validation information from JavaScript. Assuming that’s true, your jQuery would need to attach itself to the form submit and make use … Read more

Disable scrolling on “

Prevent the default behavior of the mousewheel event on input-number elements like suggested by others (calling “blur()” would normally not be the preferred way to do it, because that wouldn’t be, what the user wants). BUT. I would avoid listening for the mousewheel event on all input-number elements all the time and only do it, … Read more

Javascript number comparison fails

Because you’re comparing strings, not integers. Use parseInt to explicitly cast your values as integers. Here’s essentially what you’re doing: jsFiddle example To do the conversion, change your variables to something like: var new_da1 = parseInt( document.getElementById(‘new_da1′).value, 10); (assuming they’re all integers)