jQuery – Select Element Cheat Sheet (PDF) - Awardspace
jQuery – Select Element Cheat Sheet (PDF) - Awardspace
jQuery – Select Element Cheat Sheet (PDF) - Awardspace
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet<br />
Note: elementid refers to the actual id of the select element. elementname refers to the actual name<br />
of the select element.<br />
e.g. <br />
<strong>Select</strong>ing an element(s) for further information refer to <strong>jQuery</strong> API <strong>Select</strong>ors<br />
Basics:<br />
<strong>Select</strong> element by id $(“#elementid”)<br />
<strong>Select</strong> all select elements $(“select”)<br />
<strong>Select</strong> all select elements by class $(“select.classname”)<br />
Hierarchy:<br />
<strong>Select</strong> all options from a select element $(“#elementid option”)<br />
Basic Filters:<br />
<strong>Select</strong> the first option $(“#elementid option:first”)<br />
<strong>Select</strong> the last option $(“#elementid option:last”)<br />
<strong>Select</strong> the option at a particular index (variant 1) $(“#elementid option:eq(2)”)<br />
Attribute Filters:<br />
<strong>Select</strong> all options that have a value attribute set $(“#elementid option[value]”)<br />
<strong>Select</strong> element by name $(“select[name=elementname]”)<br />
Form Filters:<br />
<strong>Select</strong> the selected option $(“#elementid option:selected”)<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 1
<strong>jQuery</strong> core functionality, for further information refer to <strong>jQuery</strong> API Core<br />
<strong>jQuery</strong> Object Accessors:<br />
Execute a function on each matched element e.g. $(“#elementid option”).each(function() {<br />
alert(“I am an option of #elementid”);<br />
Number of matched select elements e.g. $(“select”).size() returns number of select<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 2<br />
})<br />
elements on a page<br />
Number of options in a particular select element $(“#elementid option”).size()<br />
<strong>Select</strong> an option at a particular index (variant 2) $(“#elementid option”).eq(1)<br />
Get the index of the selected option (variant 1) $("#elementid option").index($("#elementid<br />
option:selected")))<br />
<strong>Select</strong> attributes, for further information refer to <strong>jQuery</strong> API Attributes<br />
Attr:<br />
<strong>Select</strong> an attribute from matched select element e.g. select name attribute<br />
Set an attribute for matched select element e.g. set title<br />
Remove an attribute from matched select<br />
element<br />
Class:<br />
$(“#elementid”).attr(“name”)<br />
$(“#elementid”).attr(“title”, “myselect”)<br />
e.g. remove title<br />
$(“#elementid”).removeAttr(“title”)<br />
Add a class to matched select element $(“#elementid”).addClass(“inputs”)<br />
Remove a class from matched select element $(“#elementid”).removeClass(“inputs”)
HTML:<br />
Get inner HTML of matched select $(“#elementid”).html()<br />
Remove all options from matched select $(“#elementid”).html(“”)<br />
Set all new options for matched select e.g. $("#elementid").html("Some orangesMore OrangesEven more oranges")<br />
Get the text of matched option e.g. $(“#elementid option:first”).text()<br />
Get the selected text of matched select Returns text value of selected option<br />
$(“#elementid option:selected”).text()<br />
Remove the text of matched option e.g. $(“#elementid option:last”).text(“”)<br />
Set the text of a matched option e.g. $(“#elementid option:eq(2)”).text(“Purple”)<br />
Value:<br />
Get the value of the matched option item e.g. $(“#elementid option:first”).val()<br />
Get the selected value of matched select Returns value attribute of selected option<br />
$(“#elementid”).val()<br />
Set the value of a matched option e.g. $(“#elementid option:eq(2)”).val(“7”)<br />
Set the selected element based upon value i.e. Sets the selected option to option with value 7<br />
Set the selected element based upon text e.g.<br />
e.g. $(“#elementid”).val(“7”)<br />
$("#elementid").val("Oranges").attr("selected",<br />
"selected")<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 3
Traversing select attributes, for further information refer to <strong>jQuery</strong> API Traversing<br />
Finding:<br />
Get the index of the selected option (variant 2) $("#elementid option:selected").prevAll().size()<br />
Manipulating select attributes, for further information refer to <strong>jQuery</strong> API Manipulation<br />
Inserting Inside:<br />
Add options to the end of select element e.g. $("#elementid").append("Apples")<br />
Add options to the start of select element e.g. $("#elementid").prepend("Before Apples")<br />
Add options after selected index e.g. $("#elementid option:eq(0)").after("Some pears"<br />
Add options before selected index e.g. $("#elementid option:eq(3)").before("Some apricots")<br />
Replace items at a certain index e.g. $("#elementid<br />
option:eq(1)").replaceWith("Some apples")<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 4
Removing:<br />
Remove option at specified index $("#elementid option:eq(0)").remove()<br />
Remove first option $("#elementid option:first").remove()<br />
Remove last option $("#elementid option:last").remove()<br />
<strong>Select</strong> element events, for further information refer to <strong>jQuery</strong> API Events<br />
Event Helpers:<br />
Function to call when an option is selected $("#elementid").change(function() {})<br />
Getting values when an option is selected $("#elementid").change(function() {<br />
alert($(this).val());<br />
alert($(this).children("option:selected").text());<br />
<strong>Select</strong> form submission, for further information refer to <strong>jQuery</strong> API AJAX<br />
Event Helpers:<br />
Serialize select element so it can be submitted or<br />
passed in URL<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 5<br />
})<br />
$("#elementid").serialize()<br />
Returns something like this:<br />
"elementname=1"
A final note<br />
Currently there are several great tutorials that cover many ways that you can access and manipulate<br />
select elements using <strong>jQuery</strong>, but there doesn’t seem to be one place that has it all. I wanted to write<br />
this document to save people like myself who have links to about 10 different tutorials from having to<br />
go searching each time you want to remember how to do something. This document was produced<br />
after several hours of research and hard work; I hope it is a benefit to many.<br />
A big thanks to everyone for your feedback, please let me know either via email or leaving a comment<br />
on the blog if you have something kind or constructive to say.<br />
If there is anything incorrect or missing from this document please also let me know so that everyone<br />
can benefit.<br />
As you may be interested in cutting and pasting from this document to try out on your own systems<br />
please not that the content of this document and the code contained within is provided as is and is<br />
without warranty of any kind including merchantability or fitness for any other purpose whatsoever.<br />
A big thanks also to the <strong>jQuery</strong> team for making such a great product. Please go to the <strong>jQuery</strong> site for<br />
the full documentation and latest plug-ins.<br />
The myphpetc… blog can be found here http://myphpetc.blogspot.com. I keep this updated regularly<br />
so come back and visit often.<br />
- Tim Radnidge.<br />
<strong>jQuery</strong> <strong>–</strong> <strong>Select</strong> element cheat sheet Copyright 2009 <strong>–</strong> Tim Radnidge Page 6