If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript!
Dropdown select lists are a great tool in your HTML form arsenal, consisting of outer <select> tags which contain any number of <option> tags that make up the list options. Of course we all know that the value of an option doesn’t have to match what is displayed for that option, which leads us straight into today’s tip to retrieve the currently selected text string instead of the value.
The val() method in jQuery would return us the option’s value, meaning that in order to grab the text of an option, we would have to turn to the jQuery text() method.
Knowing this, the next part of this puzzle is to located the option which happens to be currently selected – and for that jQuery provides the very useful :selected selector.
The :selected selector selects all elements that are currently selected in a dropdown list, working on option tags. This means that while it will work on select boxes, it won’t function on checkboxes or radio buttons as those inputs use :checked.
So in practice, to get the text string for the currently selected option in a select dropdown list looks like this:
alert($('#mylist :selected').text());
Simple.
If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animati ...
To change the selected value, or selected index if you will, of a dropdown list using jQuery is not particularly difficult. The classic method to achieve this is to simply set the dropdown's value to a value already contained in the list using val(). (Of course this does require that the specified value is actua ...
If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animati ...
If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animati ...
Sometimes it is nice to present a multiple select listbox to a user with everything already selected. Handy if you're using listboxes as filter controls, and something that is, as per usual, quite easy to do using the magic that is the jQuery javascript library. Essentially what the aim of the game here is to set ...
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: