faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 9 people (78%) answered Yes
Recently 7 of 9 people (78%) answered Yes

Entry

I want to select some span text dynamically using Javascipt. Is there any way to do that?

Apr 8th, 2008 18:10
ha mo, Srinivas Vemula,


IE4+ and Mozilla (at least with current M0.8 so expect that to work in 
later NN6 releases) allow to change the selection, both by manipulating 
so called ranges, though the apis of IE and Mozilla differ strongly.
Here is an example
<HTML>
<HEAD>
<SCRIPT>
function selectElement (element) {
  if (document.selection) {
    var range = document.body.createTextRange();
    range.moveToElementText(element);
    range.select();
  }
  else if (window.getSelection) {
    var range = document.createRange();
    range.selectNodeContents(element);
    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
 }
}
</SCRIPT>
</HEAD>
<BODY>
<DIV ONCLICK="selectElement(this)">
Kibology for all.
</DIV>
<span ONCLICK="selectElement(this)">
All for Kibology.
</span>
</BODY>
</HTML>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com