By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,846 Members | 1,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,846 IT Pros & Developers. It's quick & easy.

Convert state of radiobutton => byte on send

P: n/a
I have asked a sortlike question on this group, but this is an other
approach for this problem.

I want to send information about a set of 8 radiobuttons to a device
that listens to TCP/IP on port 80.

I have 8 sets of radiobuttons, each has a on and off state.
When this form is send, I would like that the state of the buttons is
converted to a byte.

So button the on state of these buttons adds the following value to
the value that is send in the GET command.
Button 1 = 1
Button 2 = 2
Button 3 = 4
Button 4 = 8
Button 5 = 16
Button 6 = 32
Button 7 = 64
Button 8 = 128

So if button 1 and 8 are marked at the time the form is send, the
script adds the values of these buttons 1 + 128 = 129 and send this as
result of the form.

Can I do this easily?
Oct 18 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
In comp.lang.javascript message <ptsjf417lo2sceejf8m19perf9dreu8bfm@4ax.
com>, Sat, 18 Oct 2008 16:40:54, te** posted:
>I have 8 sets of radiobuttons, each has a on and off state.
When this form is send, I would like that the state of the buttons is
converted to a byte.
>Can I do this easily?
Apparently not. But I can : consider

<formDEMO ONLY; lacks polish
<br><input type=radio name=x1><input type=radio name=x11
<br><input type=radio name=x2><input type=radio name=x22
<br><input type=radio name=x3><input type=radio name=x34
<br><input type=radio name=x4><input type=radio name=x48
<br><input type=radio name=x5><input type=radio name=x516
<br><input type=radio name=x6><input type=radio name=x632
<br><input type=radio name=x7><input type=radio name=x764
<br><input type=radio name=x8><input type=radio name=x8128
<br>&nbsp; <input type=button value="?" onClick="Wot(this.form)">

function Wot(F) { var X=0, J=9
while (--J) X = X*2 + F["x"+J][1].checked
alert(X) }

There, F["x"+J] refers to a radiobutton pair.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

(c) John Stockton, nr London UK. ? IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:>.
<URL: maths, dates, sources.
<URL:, FAQ items, links.
Oct 18 '08 #2

P: n/a
On Oct 18, 4:40*pm, wrote:

Here's something to start playing with:

-Copy paste it and edit the line deviceURL= ""
to point to the ip of your device.
-save it as utf8.
-name it whatever.html.
-open it in the browser (better not IE).
-click the bits buttons to toggle them between 0/1
-click send and an http GET request will be sent to your device:
something like :
-in the device, extract from the url the "&data=xx" part and ignore
the rest.
-the bits are sent in text as hex: the last 2 chars of the url.
-feel free to ask any questions.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Binary & buttons</title>
<meta name="generator" content="TextMate">
<meta name="author" content="jorge">
<!-- Date: 2008-10-19 -->
button { width: 50px; height: 35px; }
.sendBtn { width: 90px; height: 35px; }
window.onload= function (event) {
var i= 0, max= 7, d= document, bits= [], e,
sendBtn= d.createElement('button');

do {
e= bits[i]= d.createElement('button');
e.bit= 1;
e.weight= Math.pow(2, max-i);
(e.onclick= bitClick).apply(e);
d.body.appendChild(d.createTextNode(i<max ? "+" : "="));
} while (++i <= max)

sendBtn.className= "sendBtn";
sendBtn.onclick= function () {
var e, deviceURL= "",
rnd= "&rnd="+ (""+Math.random()).substr(2);

e= d.createElement('img');
e.src= deviceURL+ rnd+ "&data="+ sendBtn.hex;


function bitClick (event) {
var i= bits.length, hex= 0;
this.bit= +(!this.bit);
this.innerHTML= this.bit*this.weight;
while (--i >= 0) {
hex+= +bits[i].innerHTML;
if ((sendBtn.hex= hex.toString(16)).length === 1) {
sendBtn.hex= "0"+ sendBtn.hex;
sendBtn.innerHTML= "send: 0x"+sendBtn.hex;


"Silly things such as logic, common sense and rational thoughts are
not allowed in this forum."
Oct 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.