Javascript Course

- Select the content of DIV by ID
- Select content of multiple elements with same class

This article shows you how to select the content added into HTML elements: input text field, textarea, DIV, and ather html elements.

Select text in input and textarea fields

It is simple to select the text added into an input text field or into a textarea, just apply the select() method to the input or textarea element.
element.select()
Example. When click on an input text field or textarea, selects its content.
<form action="#" method="post">
 <input type="text" name="inp1" id="inp1" value="Click to select" /><br/>
 <textarea name="txta1" id="txta1" cols="20" rows="3">Click to select</textarea>
</form>

<script>
// registers on click event to select #inp1 field data
document.getElementById('inp1').addEventListener('click', function(e){
  e.target.select();
});
  /* e.target is the the clicked element */

// registers on click event to select #txta1 textarea data
document.getElementById('txta1').addEventListener('click', function(e){
  e.target.select();
});
</script>
- Demo. Click on the input text field and textarea.

Select the content of DIV by ID

If you want to select all the content added into a DIV (or other HTML tag, like <span>, <li>, <blockquote>, etc.), use theselectElmCnt() function, presented below.
- This function receives an object with the element you want to select.

Code of the theselectElmCnt() function
// selects the content of an element. Receives the object with that element
function selectElmCnt(elm) {
  // for Internet Explorer
  if(document.body.createTextRange) {
    var range = document.body.createTextRange();
    range.moveToElementText(elm);
    range.select();
  }
  else if(window.getSelection) {
    // other browsers
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(elm);
    selection.removeAllRanges();
    selection.addRange(range);
  }
}
- Example. When click on the text added into a DIV, selects all its content.
<div id="exdv1">
 Courses for Web Programming and Development: https://CoursesWeb.net<br/>
 <span>Other line with child element.</span>
</div>

<script>
// selects the content of an element. Receives the object with that element
function selectElmCnt(elm){
  // for Internet Explorer
  if(document.body.createTextRange) {
    var range = document.body.createTextRange();
    range.moveToElementText(elm);
    range.select();
  }
  else if(window.getSelection) {
    // other browsers
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(elm);
    selection.removeAllRanges();
    selection.addRange(range);
  }
}

// registers on click event to use the selectElmCnt() function whewn click on #exdv1 div
document.getElementById('exdv1').addEventListener('click', function(){
  selectElmCnt(this);
});
</script>
Demo (click the text):
Courses for Web Programming and Development: https://CoursesWeb.net
Other line with child element.

Select content of multiple elements with same class

To select the content of multiple elements with same class, use querySelectorAll() to get an array with those elements, then, traverse the array and register "click" to each item, that calls the selectElmCnt() function presented above.
- Example:
<div class="excls">Click to select - Simple Div</div>
<pre class="excls">Content in PRE tag</pre>
<div class="excls">Another Div with same class</div>
<blockquote class="excls">Content in a blockquote html tag.</blockquote>

<script>
// selects the content of an element. Receives the object with that element
function selectElmCnt(elm) {
  // for Internet Explorer
  if(document.body.createTextRange) {
    var range = document.body.createTextRange();
    range.moveToElementText(elm);
    range.select();
  }
  else if(window.getSelection) {
    // other browsers
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(elm);
    selection.removeAllRanges();
    selection.addRange(range);
  }
}

//get array with all .excls elements
var excls = document.querySelectorAll('.excls');

//register click to each excls item that calls selectElmCnt()
for(var i=0; i<excls.length; i++){
  excls[i].addEventListener('click', function(){
    selectElmCnt(this);
  });
}
</script>
- Demo (click on these texts):
Click to select - Simple Div
Content in PRE tag
Another Div with same class
Content in a blockquote html tag.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Select the Content of HTML Element

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)