-2

When my site loads, how do I use JavaScript to select a specific set of text in a <div> (not an <input> field). Not just highlighted, but selected.

I feel this should be a REALLY easy thing to Google, but I'm failing miserably. I've found a LOT of info on how to do it for an input box. And LOTS of info on how to do it with jQuery. But, I am learning JavaScript, and don't want to learn jQuery yet.

Makyen
  • 31,849
  • 12
  • 86
  • 121
dmperkins74
  • 157
  • 3
  • 10
  • Perhaps questions like [this](http://stackoverflow.com/questions/31677451/how-to-select-div-text-on-button-click) will help you (except you'd use `window.onload` instead of a button click). – nnnnnn Mar 03 '17 at 03:54
  • Please provide example HTML and explicitly show what you want selected. Without more detail this question is too broad. You are basically asking us to *guess* at the details of what you are asking. – Makyen Mar 03 '17 at 04:29
  • 1
    Related/alternate duplicate: [Selected text inside div](http://stackoverflow.com/q/18611992) – Makyen Mar 03 '17 at 04:39
  • Are you needing help with how to run JavaScript on page load? Your question is really two separate questions:"How to run JavaScript on page load?" and "How to select text in a `
    ` with JavaScript?". The proposed duplicates only cover the latter question. The former question is something that could be asked separately, if you can't find one of the already existing answers.
    – Makyen Mar 03 '17 at 04:58

1 Answers1

0

You can try something like this:

window.addEventListener('load',function(){
    document.getElementById("text").select();
})
<input type="text" id="text" value="esf">
Hemant
  • 1,961
  • 2
  • 17
  • 27
  • While this code may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. Explanations also help you get up-votes. – Makyen Mar 03 '17 at 04:11
  • This answer is about JavaScript/HTML/CSS, so you should consider using a [snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/). Using a snippet for an answer allows people to immediately test that your answer solves the problem. Using one makes it significantly more likely that your answer will be up-voted. – Makyen Mar 03 '17 at 04:12
  • But it's not an input, it's just text in a div. Is there a way to do that? – dmperkins74 Mar 03 '17 at 04:15
  • @Makyen i hv added snippet – Hemant Mar 03 '17 at 04:32
  • @dmperkins74 is that what you want? – Hemant Mar 03 '17 at 04:32
  • @dmperkins74, The snippet helps. Just FYI: It works on Chrome, Opera and Edge, but not Firefox. – Makyen Mar 03 '17 at 04:35
  • 1
    @dmperkins74, However, this question is explicitly about selecting the text within a `
    `, and explicitly not about text in an ``. The [`.select()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select) method is not functional on `
    ` elements. It is for `HTMLInputElement` elements. To select text in a `
    ` you have to use the [selection interface](https://developer.mozilla.org/en-US/docs/Web/API/Selection) and deal with ranges, etc. See the questions/answers linked in my comments on the question wrt. proposed duplicates/related questions.
    – Makyen Mar 03 '17 at 04:44