PHP Systems
PHP Systems

Reputation: 93

"defaultValue" property for <select>?

Javascript has textObject.defaultValue=somevalue for retrieving the default value (stored value from page load) of an input even if you wipe the input and replace the contents, you can still get the default back. Like this:

// in the html page
<input id="addr1" type="text" value="21 Oak St." />

// the jquery
myInput = $("#addr1"); // the input, default value on page load = 21 Oak St.

$(myInput).val('51 New St'); // wipe default and set new

// alerts 21 Oak St
alert($(myInput).val($(myInput)[0].defaultValue));

How do you accomplish this on a select?

selectedIndex is boolean, not the value, so that does not work.

Thanks!

Upvotes: 9

Views: 21010

Answers (8)

Franco
Franco

Reputation: 2329

The simplest way to do this is:

$('#selectbox option').prop('selected', function() {
     return this.defaultSelected;
});

Upvotes: 0

Mitch McCoy
Mitch McCoy

Reputation: 1

I run this on page load to manually set the defaultValue for Select boxes.

// Set a default value property on selectboxes (for reverting)
$('select').each(function(index,ele) {
    var origvalue = $(this).val(),
        defaultvalue = $(this).prop('defaultValue');

    // If the default value hasn't already been set for this selectbox
    if(!defaultvalue) {
        $(this).prop('defaultValue', origvalue);
    }
});

Upvotes: 0

JoDev
JoDev

Reputation: 6873

I found a shortest way to do this :

$('select#myselect').find('option[defaultSelected]');

Upvotes: 0

Davious
Davious

Reputation: 1873

This is the jquery that works for me:

$('select[name="name_of_select"] option[selected]').val()

Upvotes: 6

redsd
redsd

Reputation: 11

I used the following to loop through a forum, check their default values and their current values.

if the script comes a cross a single selectbox it will loop through the children on the selectbox.

oFormObject = document.forms[0];
for(i=0; i<oFormObject.elements.length; i++)
{
     oValue = oFormObject.elements[i].value;
     oType = oFormObject.elements[i].type;
     if(oType=='select-one') {
         for(k=0; k<oFormObject.elements[i].children.length; k++)
         {
             if(oFormObject.elements[i].children[k].defaultSelected==true){
                 oformElement = oFormObject.elements[i].children[k].value;
             }
         }
     }else{
         oformElement = oFormObject.elements[i].defaultValue;
     }
     console.log(oformElement);
     console.log(oValue);
     console.log(oType);
 }

Upvotes: 1

sakhunzai
sakhunzai

Reputation: 14470

with Jquery we can do sth like this :

$('select[name="type_id"] option').map(function()
 { 
    if($(this).attr('defaultSelected')==true) return this 
 }).get(0).value;

This will return the default Selected option value :)

Upvotes: 5

Diego Perini
Diego Perini

Reputation: 1

there may be multiple "selected" option elements, in that case pick the first if no other requirements are set. @Pointy tip is correct but be aware that the "defaultValue" property can be overwritten by other code in the same page. If it were read-only it would be more useful :-)

Upvotes: 0

Pointy
Pointy

Reputation: 413682

You probably want to look at the "defaultSelected" attribute of "option" elements.

Initially, "defaultSelected" will be true if the original HTML of the option tag had an explicit "selected" attribute. You can change that by setting the attribute on option tags with Javascript after the page has loaded, in response to whatever conditions you like.

Upvotes: 18

Related Questions