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 a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.