javascripthtmltextareanicedit

Javascript: Problem with switching between html textarea and nicedit


I am using nicedit.

I have been trying to create a default html textarea that gets turned into a nicedit textfield when you click on it. I want it to revert to a plain html textarea when it loses focus. I have been able to do this successfully when using just one textarea, however, when I use two textareas strange things happen (in Firefox). I use the following script:

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript"> 

function fieldname_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_2()}
   });
}

function fieldname_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_1()}
   });
}

function fieldname2_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_2()}
   });
}

function fieldname2_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_1()}
   });
}

</script>

<TEXTAREA id="fieldname" cols="35" onclick="fieldname_1();" ></TEXTAREA>
<br><br><br>
<TEXTAREA id="fieldname2" cols="35" onclick="fieldname2_1();" >Test text</TEXTAREA>

The first textarea you click and unfocus from works perfectly, however, the second textarea you click will not disappear when trying to unfocus. What am I doing wrong?


Solution

  • You can't do the trick with more then one textarea, what about 1000 ?

    <!DOCTYPE html>
    
    <html>
        <head>
            <meta charset = "utf-8">
    
            <title></title>
    
            <script src = "http://js.nicedit.com/nicEdit-latest.js"></script>
    
            <script>
                window.onload = function () {
                    var body = document.body;
                    var limit = 1000;
    
                    for (var i = 0; i < limit; i ++) {
                        var textarea = document.createElement ("textarea");
                            textarea.style.height = "100px";
                            textarea.style.width = "100%";
    
                        body.appendChild (textarea);
                    }
    
                    // The magic
                    body.addEventListener ("click", function (event) {
                        var target = event.target;
    
                        if (target.nodeName === "TEXTAREA") {
                            var area = new nicEditor ({fullPanel : true}).panelInstance (target);
    
                            area.addEvent ("blur", function () {
                                this.removeInstance (target);
                            });
                        }
                    }, false);
                }
            </script>
    
            <style>
                textarea {
                    height: 100px;
                    margin-bottom: 20px;
                    width: 1000px;
                }
            </style>
        </head>
    
        <body>
            <!-- Create any textarea you want -->
        </body>
    </html>
    

    enter image description here