javascript - How to get the text after a radio button - Stack Overflow

Code: <input type="radio" value="x" name="a" >One<br ><

Code:

<input type="radio" value="x" name="a" />One<br />
<input type="radio" value="y" name="b" />Two<br />
....

I have many of radio buttons like above.

How can I get text string after a radio when it has been clicked?

EDITING HTML IS NOT POSSIBLE

Code:

<input type="radio" value="x" name="a" />One<br />
<input type="radio" value="y" name="b" />Two<br />
....

I have many of radio buttons like above.

How can I get text string after a radio when it has been clicked?

EDITING HTML IS NOT POSSIBLE

Share Improve this question edited Jun 28, 2011 at 11:47 thecodeparadox asked Jun 28, 2011 at 11:41 thecodeparadoxthecodeparadox 87.1k22 gold badges142 silver badges164 bronze badges 3
  • 1 You really should be using a label around those. It's more accessible, and as a bonus will make your problem easier to solve. – You Commented Jun 28, 2011 at 11:51
  • @You I know that. But editing HTML not possible. Is there any other way? Help me with someone, Friend. – thecodeparadox Commented Jun 28, 2011 at 11:59
  • stackoverflow./questions/1308509/… – Muhammad Zeeshan Commented Jun 28, 2011 at 12:11
Add a ment  | 

5 Answers 5

Reset to default 4

You could use jQuery to grab the nextSibling data from that since the text is not part of the current input tag and isn't wrapped in any tags.

jQuery:

$(document).ready(function() {
    $("input[type=radio]").click(function(e) {
        var radioText = e.currentTarget.nextSibling.data
        alert(radioText);
    });
});

EDIT: Little late for another edit but you could use labels or wrap your text in a p or span tag for making it easier to target with jQuery

<input type="radio" value="x" name="a" /><span>One</span><br />
<input type="radio" value="y" name="b" /><span>Two</span><br />

    (function(){
        var inputs = $('input[type=radio]');
        inputs.each(function(i, elm){
            console.log($(elm).next().html());
        })
    })();

You will see in the log the text of the span elements. (in the console)

this should work:

var radioButtonNames = ["a", "b"];  // Add the names of your other radio buttons 
for (var i = 0; i < radioButtonNames.length; i++) {
    document.getElementById(radioButtonNames[i]).onclick = function(){
        var result = this.nextSibling.data;
        alert(result);
    }
}

That text belongs to the <body> tag or its closest parent. I'd wrap the text in a <span> then do a $(this).next('span').text() inside your radio button's click event handler.

var radioLength = radioObj.length;
if(radioLength == undefined)
    if(radioObj.checked)
        return radioObj.value;
    else
        return "";
for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
        return radioObj[i].value;
    }
}

this will get the radio object value out, then you can use the previous answer to pull the response into a or div


Please use object.innerHTML or object.innerText methods

In your case JS would be like.

var text = $('a').innerHTML;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920985a4601130.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信