jquery

A little dot that made me pull my hair off

Posted by Danny Herran on Oct 15, 2010 in Frontend, Other Stuff | No comments

I was coding a long signup form, a very nice one, with the jQuery Validation Plugin, but it wasn’t working. I started to strip the code, piece by piece so I can identify what was causing the plugin to ignore the form and I came up with the most stupid error you will ever seen in your life (at least I think so).

Yes, a little dot that I don’t even know how to write with my keyboard. I even tried to replicate it and I couldn’t. 2 hours of coding wasted on this little bastard. Can you believe that?…

Element indexes and jQuery

Posted by Danny Herran on Aug 10, 2010 in Frontend | No comments

Once the DOM is structured by the browser, each element in our HTML page has an index relative to its parent. Retrieving an element’s index with jQuery is an easy task, and it is so useful that it can save you a couple of lines of code.

Change selected option by value on select list with jQuery

Posted by Danny Herran on Aug 10, 2010 in Frontend | 4 comments

You have a select element, and you need to “select” one of its options based on one of its values. What you do is use the “selected-selector” of jQuery to do it in a single line.

Lets say I have the following select element and I need to dynamically select the option with a value of 3, which would be the “Peach”.

<select name="myselect" id="myselect">
	<option value="1">Apple</option>
	<option value="2">Pear</option>
	<option value="3">Peach</option>
	<option value="4">Orange</option>
</select>

 

$("#myselect option[value=3]").attr('selected', 'selected');

// Or just...
$("#myselect").val(3);

If you want to get a little bit more technical, make sure you read the official jQuery documentation regarding this subject.