javascript

jQuery: Attribute Starts With Selector

Selects elements that have the specified attribute with a value beginning exactly with a given string.

For example in following example, I am trying to get values of all Select those ids start with “dd_extra_”.

HTML

<div id="form-group-other-properties" class="col-md-12">
    <div class="form-group mt-2">
        <label for="dd_extra_memory">Memory</label>
        <select class="form-control" id="dd_extra_memory">
            <option>Select Memory</option>
            <option value="64GB">64GB</option>
            <option value="128GB">128GB</option>
            <option value="256GB">256GB</option>
            <option value="523GB">523GB</option>
        </select>
    </div>
    <div class="form-group mt-2">
        <label for="dd_extra_color">Color</label>
        <select class="form-control" id="dd_extra_color">
            <option>Select Color</option>
            <option value="red">red</option>
            <option value="green">green</option>
            <option value="blue">blue</option>
            <option value="purple">purple</option>
            <option value="sky blue">sky blue</option>
            <option value="silver">silver</option>
        </select>
    </div>
</div>

jQuery

$.each($("select[id^='dd_extra_']"), function(key, element) {
    console.log($(element).val()); // printing the value of element    console.log($(element).attr('id')); // printing the actual id of the element
});

Official documentation link, click here

Author: Danyal
I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.