Results 1 to 2 of 2

Thread: event listener not working

  1. #1
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default event listener not working

    Hi, im trying to make a VERY simple CMS functionallity, i basically dynamically create a table and to each element of tabular data, i assign an onclick listener that runs a function turning the data into an input field (i used textarea) and fills the text area with the original data.

    I have this working a little bit, but i basically want to update my database onBlur of the field, but whenever i try and assign an onBlur listener to my dynamically created input field it fires straight away and breaks it...

    code is below, if anyone can help me figure out how to at least change the data to an input field (like i have) then change it back to plain text onBlur that would be great.. ill handle all the db updating etc,

    [CODE]
    <html>
    <head>
    <title>testing</title>
    <meta http-equiv="Content-Type" content="text/php; charset=UTF-8" />
    <script type="text/javascript">
    function cms(element){
    var field = new Object;
    field.content = element.innerHTML;
    field.id = element.id;
    field.primary = element.name;
    field.dom = document.getElementById(element.id);
    field.length = element.innerHTML.length +10;
    field.dom.onclick=null;

    field.edit = document.createElement("textarea");
    field.edit.cols = field.length;
    field.edit.style.height = "20px";
    field.edit.innerHTML = field.content;
    field.edit.onBlur = cms_update(field); //seems to run automatically???
    field.dom.innerHTML="";
    field.dom.appendChild(field.edit);
    }

    function cms_update(field){
    //field.dom.removeChild(field.edit);
    //field.dom.innerHTML = field.content;
    //going to upload edited field to the database from here..
    }
    </script>
    </head>
    <body>
    <table border="1">
    <tr>
    <td>Table Data</td>
    <td>Click to edit</td>
    </tr>
    <tr>
    <td id='fieldname1' name='groupIdentifier' onclick='cms(this)'>fieldname1</td>
    <td id='fieldname2' name='groupIdentifier' onclick='cms(this)'>fieldname2</td>
    </tr>
    </table>
    </body>
    </html>
    [ICODE]

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <html>
    <head>
    <title>testing</title>
    <meta http-equiv="Content-Type" content="text/php; charset=UTF-8" />
    <script type="text/javascript">
    function cms(element){
    var field = new Object;
    field.content = element.innerHTML;
    field.id = element.id;
    field.primary = element.name;
    field.dom = document.getElementById(element.id);
    field.length = element.innerHTML.length +10;
    field.dom.onclick=null;
    
    field.edit = document.createElement("textarea");
    field.edit.cols = field.length;
    field.edit.style.height = "20px";
    field.edit.innerHTML = field.content;
    field.edit.onblur =function(){ cms_update(field); }//seems to run automatically???
    field.dom.innerHTML="";
    field.dom.appendChild(field.edit);
    }
    
    function cms_update(field){
     alert(field)
    //field.dom.removeChild(field.edit);
    //field.dom.innerHTML = field.content;
    //going to upload edited field to the database from here..
    }
    </script>
    </head>
    <body>
    <table border="1">
    <tr>
    <td>Table Data</td>
    <td>Click to edit</td>
    </tr>
    <tr>
    <td id='fieldname1' name='groupIdentifier' onclick='cms(this)'>fieldname1</td>
    <td id='fieldname2' name='groupIdentifier' onclick='cms(this)'>fieldname2</td>
    </tr>
    </table>
    </body>
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •