javascript - How to get the id of all paragraph which are selected? -


i working android webview , achieving functionality need integrate javascript. new javascript please tell me how paragraphs id(id of each paragraph under selection). in more details:

like having data-

   <p id="01">hhh dhghdg       <span id="1.1">dada</span>     <span id="1.2">dgsdd</span> </p>     <p id="02">dsdgs dhgd hdhd  <span id="2.1">dada</span>     <span id="2.2">dgsdd</span> </p> 

so select data i.e "hhh dhghdg dsdgs" data belongs multiple paragraph p1 , p2 want id of both paragraph using javascript.

please tell me how this.

ppk has detailed post: http://www.quirksmode.org/dom/range_intro.html

which describes range objects. i've not done before looks of things you'll need use:

window.getselection() 

which should return selection object can range object contain selected elements (as described in link above)

from scanning docs on mdn: https://developer.mozilla.org/en-us/docs/dom/window.getselection

you'll html string attempt ids from, using regular expression, this:

var underselection = window.getselection().getrangeat(0); var idsregex = /#\s(id|class)="[^"]+"/; var idmatches = idsregex.match(underselection); //then clean , ids 

i haven't tested of i'm hoping gets started.

edit: additional info if you're using jquery might able load selection jquery object , ids out doing :

var ids = []; $(underselection).find("p").each(function(){ ids.push(this.id); }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -