I want to get first element or any element of body using index
<body>
<div id="popUpWindow" style="width: 500px; height: 200px; background-color: red; "></div>
<button id="myButton">ClickOn</button>
</body>
$(document).ready(function(){
var elem = $('body')[0];
console.log(elem);
});
I am able to get body as first with [0] index, Next I want to get the div or button in the body with the same way, I know there is a way of doing it with first child but I want to do it this way. In jquery only please
You can use the child selector or descendant selector:
$(document).ready(function(){
var elem = $('body').children().first().get(0);
console.log(elem);
});
Adding the .get(0)
will get the HTMLElement
, if not, it would be jQuery Object
.
Snippet
$(document).ready(function(){ var elem = $('body').children().first().get(0); console.log(elem); });
<div>I guess I am first object?</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
If you open up the console and see, you can find the <div>
element, being output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.