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 ?
 
The simplest way is to set an id to each element:
Code: Copy
<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:
Code: Copy
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
The simplest way is to set an id to each element:
Code: Copy
<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
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:
Code: Copy
<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>

Code: Copy
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
Back
Top