javascript to get next-previous paragraph of selected text in web page -


hello want have function paragraph of selected text.

this function current paragraph of selected text

function getselected() {   var userselection;   if (window.getselection) {       selection = window.getselection();   } else if (document.selection) {       selection = document.selection.createrange();   }   var parent = selection.anchornode;   parent = parent.parentnode;   alert(parent.innerhtml);  } 

how can next-previous paragraph of selected text in web page. if have function current paragraph above. (i think use nextsibling don't know implement in code above) can me?

-thank you-

you can use previoussibling , nextsibling parameters.

html:

<p> 1st paragraph</p> <p> 2nd paragraph</p> <p> 3rd paragraph</p> <a href="javascript:void(0)" onclick="getselected(-1)">prev</a> <a href="javascript:void(0)" onclick="getselected(1)">next</a> 

javascript:

function getselected(direction) {   var userselection;   if (window.getselection) {       selection = window.getselection();   } else if (document.selection) {       selection = document.selection.createrange();   }   var parent = selection.anchornode;   parent = parent.parentnode;   if(direction == -1){     var prevele = parent.previoussibling;     while(prevele.nodetype!=1){         prevele = prevele.previoussibling;     }     alert(prevele.innerhtml);   }else {     var nextele = parent.nextsibling ;     while(nextele.nodetype!=1){         nextele = nextele.nextsibling;     }     alert(nextele.innerhtml);   } } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -