Reputation: 2399
How would I assign the setStart and setEnd range to the first element (could be text, an image, hyperlink...) in the document?
Upvotes: 1
Views: 757
Reputation: 324627
If you mean the first element within the body of the document, I'd suggest the following. It uses setStartBefore()
in case the element is one that cannot have children, such as <img>
or <br>
:
var range = document.createRange();
var firstElInBody = document.body.getElementsByTagName("*")[0];
if (firstElInBody) {
range.setStartBefore(firstElInBody);
range.collapse(true);
}
One final thing: in case you're not aware, IE < 9 does not support Range
.
Upvotes: 3