Tihomir Grozev
Tihomir Grozev

Reputation: 1

replace() tag did not work in javascript

I have database, that currently use inline edit in tables First problem was with paste of formatted text, but seems, that problem was fixed at 90% with this script:

<script type="text/javascript">

                var _onPaste_StripFormatting_IEPaste = false;

                function OnPaste_StripFormatting(elem, e) {

                    if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) {
                        e.preventDefault();
                        var text = e.originalEvent.clipboardData.getData('text/plain');
                        window.document.execCommand('insertText', false, text);
                    }
                    else if (e.clipboardData && e.clipboardData.getData) {
                        e.preventDefault();
                        var text = e.clipboardData.getData('text/plain');
                        window.document.execCommand('insertText', false, text);
                    }
                    else if (window.clipboardData && window.clipboardData.getData) {
                        // Stop stack overflow
                        if (!_onPaste_StripFormatting_IEPaste) {
                            _onPaste_StripFormatting_IEPaste = true;
                            e.preventDefault();
                            window.document.execCommand('ms-pasteTextOnly', false);
                        }
                        _onPaste_StripFormatting_IEPaste = false;
                    }

                }

    </script>

My php code look like this:

<td contenteditable='true' onblur=saveToDatabase(this,'titleeng','".$data['id']."') onClick='showEdit(this);' onpaste='OnPaste_StripFormatting(this, event);'>".$data['titleeng']."</td>

The script removes the tags, but leaves the &nbsp; , that causing my sql ajax to failure

Here is Ajax script:

<script>
        function showEdit(editableObj) {
            $(editableObj).css("background","#FFF");
        } 

        function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#12ff65 url(loaderIcon.gif) no-repeat right");
            $.ajax({
                url: "saveedit.php",
                type: "POST",
                data:"column="+column+"&editval="+editableObj.innerHTML+"&id="+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                }        
           });
        }
        </script>

I am trying this: New components are text = text.replace("&nbsp"," ");

var _onPaste_StripFormatting_IEPaste = false;

                function OnPaste_StripFormatting(elem, e) {

                    if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) {
                        e.preventDefault();
                        var text = e.originalEvent.clipboardData.getData('text/plain');
                        string text = text;
                        text = text.replace("&nbsp"," ");
                        window.document.execCommand('insertText', false, text);
                    }
                    else if (e.clipboardData && e.clipboardData.getData) {
                        e.preventDefault();
                        var text = e.clipboardData.getData('text/plain');
                        string text = text;
                        text = text.replace("&nbsp"," ");
                        window.document.execCommand('insertText', false, text);
                    }
                    else if (window.clipboardData && window.clipboardData.getData) {
                        // Stop stack overflow
                        if (!_onPaste_StripFormatting_IEPaste) {
                            _onPaste_StripFormatting_IEPaste = true;
                            e.preventDefault();
                            window.document.execCommand('ms-pasteTextOnly', false);
                        }
                        _onPaste_StripFormatting_IEPaste = false;
                    }

                }

But nothing

Upvotes: 0

Views: 83

Answers (2)

Tihomir Grozev
Tihomir Grozev

Reputation: 1

finally problem was with encoding as CBroe says

here is the ajax, that make it work

function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#12ff65 url(loaderIcon.gif) no-repeat right");
            var editxx = encodeURIComponent(editableObj.innerHTML);
            $.ajax({
                url: "saveedit.php",
                type: "POST",
                data:"column="+column+"&editval="+editxx+"&id="+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                }        
           });
        }

Upvotes: 0

C3roe
C3roe

Reputation: 96250

The script removes the tags, but leaves the &nbsp; , that causing my sql ajax to failure

That‘s because you neglected to URL-encode the parameter values you insert into the query string properly.

Use encodeURIComponent on the values before.

Upvotes: 1

Related Questions