B4J Question [WebApp] Which options is selected

derez

Expert
Licensed User
Longtime User
I added radio buttons to the index.html
<input type="radio" name="mode" value="left">Left<br>
<input type="radio" name="mode" value="right">Right
How do I check in the websocket which button is checked ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The simplest way is to set an id to each element:
B4X:
<div id="maindiv">
  <input type="radio" name="sex" id="radioleft">Left<br/>
     <input type="radio" name="sex" id="radioright">Right<br/>
  <button id="btncalc">Calculate</button><br/>
  <p id="result"></p>
  </div>
Then in your code:
B4X:
Sub Class_Globals
   Private ws As WebSocket 'ignore
   Private RadioLeft, RadioRight, btnCalc, Result As JQueryElement
End Sub

Public Sub Initialize
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
   Log("Connected")
   ws = WebSocket1
End Sub

Sub btnCalc_Click (Params As Map)
   Dim fleft As Future = RadioLeft.GetProp("checked")
   Dim fright As Future = RadioRight.GetProp("checked")
   If fleft.Value = True Then
     Result.SetText("left is checked")
   Else If fright.Value Then
     Result.SetText("right is checked")
   End If
End Sub

There are other possible ways. See this link: http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
The simplest way is to set an id to each element:
B4X:
<div id="maindiv">
  <input type="radio" name="sex" id="radioleft">Left<br/>
     <input type="radio" name="sex" id="radioright">Right<br/>
  <button id="btncalc">Calculate</button><br/>
  <p id="result"></p>
  </div>

I prefer Right sex. Left sex is just wrong.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Oops... :)

It started with an example that included male and female values.

In this case we don't need to check each of the elements, instead we let jQuery to find the checked element:
B4X:
<div id="maindiv">
  <input type="radio" name="sex" value="male">Male<br/>
     <input type="radio" name="sex" value="female">Female<br/>
  <button id="btncalc">Calculate</button><br/>
  <p id="result"></p>
  </div>

B4X:
Sub btnCalc_Click (Params As Map)
   Dim jq As JQueryElement = ws.GetElementBySelector("input[name=sex]:checked")
   Dim fval As Future = jq.GetVal
   Result.SetText("Selected value: " & fval.Value)
End Sub
 
Upvote 0
Top