Select Option Auto Set

by Ivan Gerasimenko

HTML

<select id="one-val">
    <option >&lt;select value&gt;</option>
    <option value="0">text 1</option>
</select>
<br />
<select id="two-val">
    <option>&lt;select value&gt;</option>
    <option value="0">text 1</option>
    <option value="1">text 2</option>
</select>
<br />
<input id="input-field" />

JavaScript

var log = function (msg) { console.log(msg); };

// value - The textual content of this attribute represents the label explaining the option. If it is not defined, its default value is the text content of the element.
log("val: " + $("#one-val").val());

$.fn.autoSet = function() {
    if ($(this).prop("tagName") !== "SELECT") {
        return "The element does not support method getInfo\n" + this;
    }
    
    log(": " + $(this).prop("tagName"));
    log("  " + ($(this).prop("tagName")==="SELECT"));
    log($(this).find("option"));
    
    $.each($(this).find("option"), function (index, value) {
        log(index + " : " 
            + value.outerHTML + " : " 
            + (value.outerHTML.indexOf("value") > -1));
    });
}


log($("#one-val").autoSet());
log($("#input-field").autoSet());