jQuery Event Keypress: Which key was pressed?
Actually this is better: var code = e.keyCode || e.which; if(code == 13) { //Enter keycode //Do something }
Actually this is better: var code = e.keyCode || e.which; if(code == 13) { //Enter keycode //Do something }
Bind to the change event instead of click. However, you will probably still need to check whether or not the checkbox is checked: $(“.checkbox”).change(function() { if(this.checked) { //Do stuff } }); The main benefit of binding to the change event over the click event is that not all clicks on a checkbox will cause it … Read more
Just use. $(‘input[name=”name_of_your_radiobutton”]:checked’).val(); It is that easy.
I use this small function for the same purpose, executing a function after the user has stopped typing for a specified amount of time or in events that fire at a high rate, like resize: function delay(callback, ms) { var timer = 0; return function() { var context = this, args = arguments; clearTimeout(timer); timer … Read more
Basic usage of .ajax would look something like this: HTML: <form id=”foo”> <label for=”bar”>A bar</label> <input id=”bar” name=”bar” type=”text” value=”” /> <input type=”submit” value=”Send” /> </form> jQuery: // Variable to hold request var request; // Bind to the submit event of our form $(“#foo”).submit(function(event){ // Prevent default posting of form – put here to work … Read more
jQuery now defines a when function for this purpose. It accepts any number of Deferred objects as arguments, and executes a function when all of them resolve. That means, if you want to initiate (for example) four ajax requests, then perform an action when they are done, you could do something like this: $.when(ajax1(), ajax2(), … Read more
Same-origin policy You can’t access an <iframe> with different origin using JavaScript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin. Origin is considered different if at least one of the following parts of the address … Read more
Use the .scroll() event on window, like this: $(window).scroll(function() { if($(window).scrollTop() + $(window).height() == $(document).height()) { alert(“bottom!”); } }); You can test it here, this takes the top scroll of the window, so how much it’s scrolled down, adds the height of the visible window and checks if that equals the height of the overall … Read more
Best solution here. var getUrlParameter = function getUrlParameter(sParam) { var sPageURL = window.location.search.substring(1), sURLVariables = sPageURL.split(‘&’), sParameterName, i; for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split(‘=’); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]); } } return false; }; And this is how you can use … Read more
You have to tell replace() to repeat the regex: .replace(/ /g,”) The g character makes it a “global” match, meaning it repeats the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here. If you want to match all whitespace, and not just the literal space character, use \s … Read more