/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Reading radio button text information

In the following script, I cannot figure out how to access the TEXT information
of the radio buttons displayed. All other coded functions perform as expected.

Can someone show me the error my my ways since I’m not receiving any from the browser? ?

[code]
<html>
<head>
<title>Options Text</title>
<script type=”text/javascript”>
function SboxVOptions(IDS) {
var tarr = [];
var sel = document.getElementById(IDS);
for (var i=0; i<sel.options.length; i++) { tarr.push(sel.options[i].value); }
alert(tarr.join(‘n’));
}

function SboxTOptions(IDS) {
var tarr = [];
var sel = document.getElementById(IDS);
for (var i=0; i<sel.options.length; i++) { tarr.push(sel.options[i].text); }
alert(tarr.join(‘n’));
}

function RBtnVOptions(GrpName) {
var tarr = [];
var sel = document.getElementsByName(GrpName);
for (var i=0; i<sel.length; i++) { tarr.push(sel[i].value); }
alert(tarr.join(‘n’));
}

function RBtnTOptions(GrpName) { // this function nor alert not working correctly ???
var tarr = [];
var sel = document.getElementsByName(GrpName); // alert(sel.join(‘n’));
for (var i=0; i<sel.length; i++) { tarr.push(sel[i].text); }
alert(tarr.join(‘n’));
}

</script>
</head>
<body>
<form name=”myForm” action=”” onsubmit=”return false”>
<select id=”sboxInfo” onchange=”alert(this.value)”>
<option value=”0″> zero </option>
<option value=”1″> one </option>
<option value=”2″> two </option>
<option value=”3″> three </option>
<option value=”4″> four </option>
<option value=”5″> five</option>
<option value=”6″> six</option>
<option value=”7″> seven</option>
</select>
<button onclick=”SboxVOptions(‘sboxInfo’)”>SBox Option Values </button>
<button onclick=”SboxTOptions(‘sboxInfo’)”>SBox Option Text </button>
<br><h3>Above works fine</h3>

<p>
<h3>RBtn Option Text below needs work ???</h3>
<input type=”radio” name=”rBtn” value=”000″ onclick=”alert(this.value)”> ZERO </input>
<input type=”radio” name=”rBtn” value=”001″ onclick=”alert(this.value)”> ONE </input>
<input type=”radio” name=”rBtn” value=”010″ onclick=”alert(this.value)”> TWO </input>
<input type=”radio” name=”rBtn” value=”011″ onclick=”alert(this.value)”> THREE </input>
<input type=”radio” name=”rBtn” value=”100″ onclick=”alert(this.value)”> FOUR </input>
<input type=”radio” name=”rBtn” value=”101″ onclick=”alert(this.value)”> FIVE </input>
<input type=”radio” name=”rBtn” value=”110″ onclick=”alert(this.value)”> SIX </input>
<input type=”radio” name=”rBtn” value=”111″ onclick=”alert(this.value)”> SEVEN </input>
<br>
<button onclick=”RBtnVOptions(‘rBtn’)”>RBtn Option Values </button>
<button onclick=”RBtnTOptions(‘rBtn’)”>RBtn Option Text </button>
</form>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineFeb 13.2010 — In the following script, I cannot figure out how to access the TEXT information

of the radio buttons displayed. All other coded functions perform as expected.
[/QUOTE]
Radio buttons have no [I]text[/I] property but you could read the text from the adjacent element:[CODE]function RBtnTOptions(GrpName)
{
var tarr = [];
var sel = GrpName; // alert(sel.join('n'));
for (var i=0; i<sel.length; i++) { tarr.push(sel[i].nextSibling.nodeValue.replace(/[nr]/g,'')); }
alert(tarr.join('n'));
}

// Within the form's scope, the group name can be passed as a variable:

<button onclick="RBtnTOptions( rBtn )">RBtn Option Text </button>
[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorFeb 13.2010 — Thank you 'Sterling Isfine', works just like I wanted.

I do have a question. I had tried something similar in an earlier attempt, but noticed you do something slightly different.

I called the function with

<button onclick="RBtnTOptions('rBtn')">RBtn Option Text </button>

while you use

<button onclick="RBtnTOptions(rBtn)">RBtn Option Text </button>

The main difference I see is the single quotes around 'rBtn'.

I don't get errors with my version, but I also don't get a display as when I use your version.

I just wanted to know if you know why this is happening with the difference you posted?

Really do appreciate the answer. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...