• Parent 1
    • Child 1
    • Child 2
      • Parent 1
        • Child 1
        • Child 2
          • Parent 1
            • Child 1
            • Child 2
            • DEVHIDE
              • Home (current)
              • About
              • Contact
              • Cookie
              • Home (current)
              • About
              • Contact
              • Cookie
              • Disclaimer
              • Privacy
              • TOS
              Login Or Sign up

              Set element ID on page load with Jquery?

              3.4k Views Asked by Nấm Lùn At 05 December 2013 at 11:16 2025-12-16T07:55:21.362956

              Assume that I have some HTML elements like these:

              <div id="wrapper">
                  <ul>
                      <li><a>Parent 1</a>
                          <ul>
                               <li><a>Child 1</a></li>
                               <li><a>Child 2</a></li>
                               <li><a>Child 3</a></li>
                          </ul>
                      </li>
                      <li><a>Parent 2</a>
                          <ul>
                               <li><a>Child 1</a></li>
                               <li><a>Child 2</a></li>
                               <li><a>Child 3</a></li>
                          </ul>
                      </li>
                  </ul>
              </div>
              

              How can I set the ID for the first UL element in the #wrapper when $(document) ready??

              Thanks in advance!

              jquery set attr pageload document-ready
              Original Q&A
              4

              There are 4 best solutions below

              0
              GautamD31 GautamD31 On 05 December 2013 at 11:17 BEST ANSWER

              Try with :first like

              $('#wrapper ul:first').attr('id', 'customeId');
              

              Or you can try with :eq(0) like

              $('#wrapper ul:eq(0)').attr('id', 'customeId');
              

              Even(May be) you can try with .eq(0) like

              $('#wrapper ul').eq(0).attr('id', 'customeId');
              

              Make sure that you have this script on DOM ready like

              $(document).ready(function(){
                  $('#wrapper ul:first').attr('id', 'customeId');
              });
              

              And please correct your html because it is breaking while closing ul

              0
              Adil Adil On 05 December 2013 at 11:16

              Like this.

              Live Demo

              $(document).ready(function(){
                  $('#wrapper ul:first').attr('id', 'yourid');
              });
              

              You have invalid html as well, you missed the closing ul

              <div id="wrapper">
                  <ul>
                      <li><a>Parent 1</a>
                          <ul>
                               <li><a>Child 1</a></li>
                               <li><a>Child 2</a></li>
                               <li><a>Child 3</a></li>
                          </ul>    
                      </li>
                      <li><a>Parent 2</a>
                          <ul>
                               <li><a>Child 1</a></li>
                               <li><a>Child 2</a></li>
                               <li><a>Child 3</a></li>
                          </ul>     
                      </li>
                  </ul>
              </div>
              
              0
              Anoop Joshi P Anoop Joshi P On 05 December 2013 at 11:18
                 $(document).ready(function () {
                  $("#wrapper").children("ul").attr("id", "myid");
                 });
              
              0
              Prateek Prateek On 05 December 2013 at 11:18

              Use .attr

              $("#wrapper ul:first").attr("id","id");
              

              Related Questions in JQUERY

              • How to sort these using Javascript or Jquery Most effectively
              • Ajax jQuery firing multiple time display event for the same result
              • .hover() seems to overwrite .click()
              • Check for numeric value with optional commas javascript
              • Extending Highmaps Side Effect
              • Array appending after each onclick and loop in javascript
              • how can i append part of a table based on how many tr it has?
              • Play multiple audio files in a slider
              • Remove added set of rows
              • Access property of an object of type [Model] in JQuery
              • AJAX PHP - Reload div after submit
              • proengsoft/laravel-jsvalidation ReferenceError: jQuery is not defined
              • when a checkbox is checked how to display a different hidden element using javascript
              • Get jquery error Uncaught RangeError: Maximum call stack size exceeded
              • Removing only the closest thead on table filtering

              Related Questions in SET

              • Removing duplicates from arraylist using set
              • Order-independent Hash Algorithm
              • How to count 2 different duplicate values in array - Swift
              • CMD specifying columns to save?
              • comparison of two sets when repeated in r
              • How to use set(data structure) in mongodb console?
              • Reversing logic of a product-country mapping
              • Storing data in sorted manner in a HashSet
              • Generate all combinations of strings and their substrings in a set -- python
              • How to access class data in a set of pointers to that class
              • Scala set element uniqueness: What to implement for comparison of user defined classes?
              • java.util.Set add and remove method signature difference
              • retrieve all the smallest strings from a set in python
              • Setting a String Variable in SSIS
              • Setting a robocopy log file to a variable

              Related Questions in ATTR

              • jQuery retrieve multiple .attr() on page
              • Remote attribute for validation in MVC not working
              • wxpython wxgrid attributes
              • use jquery to get id of input field to pass back color change
              • Custom Attributes for TextView Subclass not working
              • jquery $(this).attr('href') returns undefined
              • Add attributeif ng-if is false. angular
              • CSS data-content attribute escapes backslashes
              • Can't get info on os.path attribute in python
              • Jquery onmouseover not working
              • Can I pull different data values with same attribute name, with just one click button?
              • How to add input's value as a class to parent
              • CSS set background-image by data-image attr
              • How to add rel attribute when img src not empty?
              • How to use attributes to change text color from activity

              Related Questions in PAGELOAD

              • Run script on page load
              • Implementing loading div in a php page not working
              • Set session-state Cookiemode property for click event?
              • In an ASP.NET web form, how to determine what causes a Page_Load()
              • how to handle 1000+ images in one web page?
              • On document ready get cities by country id not working [ jquery custom plugin ]
              • pageLoad function in page and masterpage
              • JQM 1.4.3 refresh submenu panel after AJAX load
              • how to dropdown option keep selected after page loads onchange
              • How to stop selenium webdriver from waiting for page load?
              • ASP.NET listbox selectedValue in PageLoad
              • How to call a function when a page loads in Javascript with Jquery
              • Set element ID on page load with Jquery?
              • clarification on when does ajax call within document.ready gets triggered
              • Page_Init and Page_Load executes twice ASP & VB.NET only in IE .. why?

              Related Questions in DOCUMENT-READY

              • Div going through fade in/fade out cycle blinks abruptly
              • Need help finding a bug with jquery $(document).ready() not working properly in firefox
              • function runs on document.ready instead of when told to
              • Enforce DOMContentLoaded event on Window before jQuery Document ready
              • Is $(function () {}) same to $(document).ready(function(){});
              • InnerHTML - Self invoking function works, but why doesn't Jquery document ready work?
              • javascript loading before jQuery Ready?
              • Beginner javascript/jquery - document.write
              • Set element ID on page load with Jquery?
              • IE: execute jQuery plugin in popup window when document ready
              • jQuery, and other JS not running when copied from working jsfiddle into local env?
              • Document ready start one function but not the other function unify
              • document.readyState is not changing in Chrome and Mozilla but changes in IE
              • Pretty Photo wont trigger when using .load?
              • jQuery does not execute on dynamically loaded content, even on a click events

              Trending Questions

              • UIImageView Frame Doesn't Reflect Constraints
              • Is it possible to use adb commands to click on a view by finding its ID?
              • How to create a new web character symbol recognizable by html/javascript?
              • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
              • Heap Gives Page Fault
              • Connect ffmpeg to Visual Studio 2008
              • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
              • How to avoid default initialization of objects in std::vector?
              • second argument of the command line arguments in a format other than char** argv or char* argv[]
              • How to improve efficiency of algorithm which generates next lexicographic permutation?
              • Navigating to the another actvity app getting crash in android
              • How to read the particular message format in android and store in sqlite database?
              • Resetting inventory status after order is cancelled
              • Efficiently compute powers of X in SSE/AVX
              • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

              Popular # Hahtags

              javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

              Popular Questions

              • How do I undo the most recent local commits in Git?
              • How can I remove a specific item from an array in JavaScript?
              • How do I delete a Git branch locally and remotely?
              • Find all files containing a specific text (string) on Linux?
              • How do I revert a Git repository to a previous commit?
              • How do I create an HTML button that acts like a link?
              • How do I check out a remote Git branch?
              • How do I force "git pull" to overwrite local files?
              • How do I list all files of a directory?
              • How to check whether a string contains a substring in JavaScript?
              • How do I redirect to another webpage?
              • How can I iterate over rows in a Pandas DataFrame?
              • How do I convert a String to an int in Java?
              • Does Python have a string 'contains' substring method?
              • How do I check if a string contains a specific word?
              .

              Copyright © 2021 Jogjafile Inc.

              • Disclaimer
              • Privacy
              • TOS
              • Homegardensmart
              • Math
              • Aftereffectstemplates