locked
Get attribute Id value in Jquery RRS feed

  • Question

  • User82362805 posted

    I have two file up-loader  (which can be 3 or 4 generated dynamically) have the same class named as 'image' and every fileuploader has different id.

    I want to get the value of id for each fileuploader of calss=image on ready event. How would I do that? 

    Note: the value of Id also generated dynamically and available in DOM at ready event.

    Thursday, August 16, 2018 9:39 AM

Answers

  • User475983607 posted

    I want to get the value of id for each fileuploader of calss=image on ready event. How would I do that? 

    Learn jQuery selectors and $.each().  There's also $.map().

    https://api.jquery.com/category/selectors/

        <script>
            $(function () {
                let ids = [];
    
                $('.image').each(function (index, value) {
                    ids.push($(this).prop("id"));
                });
    
                console.log(ids);   
            });
        </script>

    I have to ask... you wrote code to dynamically create DOM elements but you don't know the IDs?  How is that possible? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 11:06 AM
  • User-1171043462 posted

    This way

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $('.image').each(function () {
                    var id = this.id;
                    alert(id);
                });
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 12:40 PM

All replies

  • User475983607 posted

    I want to get the value of id for each fileuploader of calss=image on ready event. How would I do that? 

    Learn jQuery selectors and $.each().  There's also $.map().

    https://api.jquery.com/category/selectors/

        <script>
            $(function () {
                let ids = [];
    
                $('.image').each(function (index, value) {
                    ids.push($(this).prop("id"));
                });
    
                console.log(ids);   
            });
        </script>

    I have to ask... you wrote code to dynamically create DOM elements but you don't know the IDs?  How is that possible? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 11:06 AM
  • User-1171043462 posted

    This way

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $('.image').each(function () {
                    var id = this.id;
                    alert(id);
                });
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2018 12:40 PM