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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk