简体   繁体   中英

selecting text next to an checked input checkbox using JQuery (or JS)?

given the next HTML block:

<div class"radioGroup">
   <input type="radio" value="0">
        This is NOT checked
   <input type="radio" value="1" checked = "checked" > 
        This is checked
   <input type="radio" value="2">
        This is NOT checked
</div>

How can i select the text following the selected radio button? (eg "This is checked").

(I cant add tags to the strings or something like that, i'm trying to grab this text from another web page, so i can only use client side scripts)

You can use:

<input ... onclick="alert(this.nextSibling.data);">

Of course that is very dependent on the structure of the DOM. You may want to concatenate the data in all the text nodes from this element to the next, so you might need a function like:

function getTextByNodes(el) {
  var text ='';
  while (el && el.nextSibling && el.nextSibling.nodeType == 3) {
    text += el.nextSibling.data;
    el = el.nextSibling;
  }
  return text;
}

with:

<input ... onclick="alert(getTextByNodes(this));">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM