<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matthew</title>
    <description>The latest articles on DEV Community by Matthew (@matthewbaynham).</description>
    <link>https://dev.to/matthewbaynham</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2060023%2F442bb5d8-6698-4e4b-900c-c7f932db058d.png</url>
      <title>DEV Community: Matthew</title>
      <link>https://dev.to/matthewbaynham</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matthewbaynham"/>
    <language>en</language>
    <item>
      <title>how do I fix readonly &lt;input&gt;, please?</title>
      <dc:creator>Matthew</dc:creator>
      <pubDate>Wed, 11 Sep 2024 20:07:34 +0000</pubDate>
      <link>https://dev.to/matthewbaynham/how-do-i-fix-readonly-please-5g8h</link>
      <guid>https://dev.to/matthewbaynham/how-do-i-fix-readonly-please-5g8h</guid>
      <description>&lt;p&gt;I'm very new to react, this is the first react code I've ever written but I've decades experience in other technologies.&lt;/p&gt;

&lt;p&gt;I'm using the html tag  to enter data and sometimes it's read/write and sometimes it readonly, that is a problem I need them to always be read/write.&lt;/p&gt;

&lt;p&gt;I've found a post on stackover which tell me what is causing the problem but it doesn't explain how to fix the problem.  This link &lt;a href="https://stackoverflow.com/questions/28315205/why-does-react-make-inputtype-text-fields-readonly-unless-i-supply-onchange" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/28315205/why-does-react-make-inputtype-text-fields-readonly-unless-i-supply-onchange&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So in my code you can see I've two 's that work, they point to the attributes theXXX and theYYY.  However I have an array that is called lstXXXs in my attributes and I display this array, see the line beginning with const htmlLstXXXs = props.attributes.lstXXXs.map&lt;/p&gt;

&lt;p&gt;Basically because I have the two variables theXXX and theYYY working I was hoping to copy the same method for updating the array, but whatever I try to do it just won't work, the  are readonly.&lt;/p&gt;

&lt;p&gt;My function to update the array works, but the 's being readonly is the problem.&lt;/p&gt;

&lt;p&gt;Does anyone have any idea how I can fix it so that all my 's tags are read/write?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";

wp.blocks.registerBlockType("matthewbaynham/my-wordpress-plugin_stuff", {
    title: "XXX and YYY", 
    icon: "smiley", 
    category: "common",
    attributes: {
        theXXX: { type: "string" },
        theYYY: { type: "string" },
        lstXXXs: [],
    },
    edit: function (props) {
        function updateXXX(e) {
            props.setAttributes({theXXX: e.target.value});
        }

        function updateYYY(e) {
            props.setAttributes({theYYY: e.target.value});
        }

        function updateXXXID(obj, e) {
            alert(obj.id);
            alert(obj.xxx);
            alert(obj.yyy);
            alert(document.getElementById("xxx"+obj.id).value);
            alert("done");
        }

        function updateYYYID(qa) {
            props.attributes.lstXXXs.forEach(function(obj) {
                                 if (obj.id === qa.id) {
                                     const elementXXX = document.getElementById("xxx"+qa.id);

                                     if (elementXXX !== null)
                                     { obj.xxx = elementXXX.value; }

                                     const elementYYY = document.getElementById("yyy"+qa.id);

                                     if (elementYYY !== null)
                                     {obj.yyy = elementYYY.value; }
                                 }
                            });
        }

        function showArray(e) {
            var theText = '';            
            props.attributes.lstXXXs.forEach(function(obj) {
                                 theText += obj.id.toString();
                                 theText += '\n';
                                 theText += obj.xxx;
                                 theText += '\n';
                                 theText += obj.yyy;
                                 theText += '\n';
                                 theText += '\n';
                            });

            alert(theText);                
        }

        function addOneXXX(e) {
            const tmpXXXs = props.attributes.lstXXXs.concat({id:maxID()+1, xxx:"Fred", yyy:"3"});
            props.attributes.lstXXXs = tmpXXXs;

            const divMain = ReactDOM.createRoot(document.getElementById('main'));
            divMain.render(getHTML());
        }

        function maxID() {
            return Math.max(...props.attributes.lstXXXs.map(o =&amp;gt; o.id));
        }

        function getHTML() {
            const htmlLstXXXs = props.attributes.lstXXXs.map(
                qa =&amp;gt; {
                         return (
                            &amp;lt;div id={"qa"+qa.id}&amp;gt;
                                &amp;lt;p&amp;gt;{qa.id}&amp;lt;/p&amp;gt;
                                &amp;lt;input id={"q"+qa.id} type="text" placeholder="Wite" value={qa.xxx} /&amp;gt;
                                &amp;lt;input id={"xxx"+qa.id} type="text" placeholder="Write a xxx here." value={qa.xxx} onInput={() =&amp;gt; updateXXXID(qa)} /&amp;gt;
                                &amp;lt;div id="yyy" class="yyy"&amp;gt;
                                    &amp;lt;input id={"yyy"+qa.id} type="text" placeholder="This is where the yyy goes, again change this in the attributes." value={qa.yyy} onChange={() =&amp;gt; updateYYYID(qa)} /&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;
                          )
                      });

            return &amp;lt;div id="xxx" class="xxx"&amp;gt;
                    &amp;lt;div&amp;gt;
                    &amp;lt;div&amp;gt;{htmlLstXXXs}&amp;lt;/div&amp;gt;
                    &amp;lt;div onClick={addOneXXX}&amp;gt;Add to list&amp;lt;/div&amp;gt;
                    &amp;lt;div onClick={showArray}&amp;gt;Show Array&amp;lt;/div&amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;input type="text" placeholder="Write a xxx here" value={props.attributes.theXXX} onChange={updateXXX}/&amp;gt;
                    &amp;lt;div id="yyy" class="yyy"&amp;gt;
                        &amp;lt;input type="text" placeholder="This is where the yyy goes." value={props.attributes.theYYY} onChange={updateYYY} /&amp;gt;
                    &amp;lt;/div&amp;gt;
               &amp;lt;/div&amp;gt;
        }

        if (typeof(props.attributes.lstXXXs) === "undefined") {
            props.attributes.lstXXXs = [{id:1, xxx:"Maria", yyy:"17"}, 
                                             {id:2, xxx:"Bob", yyy:"2"}, 
                                             {id:3, xxx:"Ed", yyy:"88"}, 
                                             {id:4, xxx:"Wayne", yyy:"666"}]
        }

        return &amp;lt;div id='main'&amp;gt;{getHTML()}&amp;lt;/div&amp;gt;       
    },
    save: function (props) {
       return null
    }
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>discuss</category>
      <category>react</category>
      <category>html</category>
      <category>input</category>
    </item>
  </channel>
</rss>
