Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  122] [ 1]  / answers: 1 / hits: 20984  / 9 Years ago, tue, february 3, 2015, 12:00:00

I have a set of two radio buttons one of which is checked when the page is loaded. It appears that I can't change it to the second one. The HTML is build by reactjs if that matters. The markup on chrome looks like:



<fieldset data-reactid=.0.0.0.0.0.0.1>
<label for=coop-radio-btn data-reactid=.0.0.0.0.0.0.1.0>
<span data-reactid=.0.0.0.0.0.0.1.0.0>To a Cooperative</span>
<input type=radio name=requestOnBehalfOf value=coop id=coop-radio-btn data-reactid=.0.0.0.0.0.0.1.0.1>
</label>
<label for=union-radio-btn data-reactid=.0.0.0.0.0.0.1.1>
<span data-reactid=.0.0.0.0.0.0.1.1.0>Additional Request</span>
<input type=radio name=requestOnBehalfOf value=union checked= id=union-radio-btn data-reactid=.0.0.0.0.0.0.1.1.1>
</label>
</fieldset>

More From » html

 Answers
13

The reason for this was that React was making the inputs controlled components because the values for each input are set from the server side.


Quoting from Brandon's answer:


If you add a value={whatever} property to the input, which makes it a controlled component, then it is read-only uness you add an onChange handler that updates the value of whatever. From the React docs:



Why Controlled Components?


Using form components such as <input> in React presents a challenge that is absent when writing traditional form HTML. For example, in HTML:


<input type="text" name="title" value="Untitled" />

This renders an input initialized with the value, Untitled. When the user updates the input, the node's value property will change. However, node.getAttribute('value') will still return the value used at initialization time, Untitled.


Unlike HTML, React components must represent the state of the view at any point in time and not only at initialization time. For example, in React:


render: function() {
return <input type="text" name="title" value="Untitled" />;
}

Since this method describes the view at any point in time, the value of the text input should always be Untitled.



The simplest solution I found is to add onChange={function(){}} to the inputs.


[#67970] Sunday, February 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alli

Total Points: 409
Total Questions: 101
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
alli questions
Sat, Apr 23, 22, 00:00, 2 Years ago
Mon, May 18, 20, 00:00, 4 Years ago
Tue, Mar 24, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;