Jump to content

how to trim a output and insert it to a variable in javascript


narutofan

Recommended Posts

hi,
i'm trying to create a @tagging script for one of my project where i'm using at.js with tinymce editor. The problem is i get the desired username in alert output ,but can't seem to bind it to a variable that will be accepted by at.js. The response i get when i alert is

Quote

`[{"readyState":4,"responseText":"\n\n{\"names\":\"Shikhar Bansal(bshikhar13131313@gmail.com),\"}","responseJSON":{"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"},"status":200,"statusText":"OK"}]`

but i want only the

Quote

"Shikhar Bansal(bshikhar13131313@gmail.com)"

part in the variable don't know how to get it in jquery or javascript a little help in solving this problem might help me a lot. The code i have tried so far

tinymce.init({
        selector: 'textarea#wall_id_1',
        height: 300,
        theme: 'modern',
        resize: false,
        force_p_newlines: false,
        plugins: 'print preview powerpaste searchreplace autolink directionality advcode visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount tinymcespellchecker a11ychecker imagetools mediaembed  linkchecker contextmenu colorpicker textpattern help',
        toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat',
        image_advtab: true,
        templates: [
            {
                title: 'Test template 1',
                content: 'Test 1'
            },
            {
                title: 'Test template 2',
                content: 'Test 2'
            }
        ],
        content_css: [
            '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
            '//www.tinymce.com/css/codepen.min.css'
        ],
        setup: function(editor) {
            editor.on('keyup', function(e) {
            /*  
                var eli= $(editor.contentDocument.activeElement).prop('innerHTML');
                var txt=$(eli+'p').html();
                var txt1=$(eli+'span').html();
                var regex=new RegExp(/@+([a-zA-z!._-]+)/g);
                var match= regex.exec(txt); 
                // console.log(match);
                // alert(match);
                var names=load_Ajax(match[1]);
                return names;
            */
            if (e.keyCode == 13 && $(editor.contentDocument.activeElement).atwho('isSelecting'))
                return false;
            });
            return names;
        },
        init_instance_callback: function(editor) {
            var name = editor.on("keyup", function(e) {
                var eli = $(editor.contentDocument.activeElement).prop('innerHTML');
                console.log('eli=', eli);
                var txt = $(eli + 'p').html();
                console.log('txt=', txt);
                var txt1 = $(eli + 'span').html();
                console.log('txt1=', txt1);
                var regex = new RegExp(/@+([a-zA-z!._-]+)/g);
                var match = regex.exec(txt);
                console.log('match=', match);
              if( typeof match != 'undefined' && match != null ) {
          var results = [];
            $.ajax({
            url: "jsdropdown.php",
            data: {
              "uname": match[1]
            },
            async: false,
            complete: function(res) {
              results.push(res);
              // results=res;
              // alert(JSON.stringify(res));
              return res;
            },
            dataType: "json"
          });
          
          var results1=JSON.stringify(results);
          alert(results1);
          $(editor.contentDocument.activeElement).atwho({
            at: "@",
            data: results1
          });
                }
            }).responseJSON;
        }
    });

here is a js fiddle: http://jsfiddle.net/e8szo2yw/8/

Link to comment
Share on other sites

Take what you have, json_decode it

$json = '[{"readyState":4,"responseText":"\n\n{\"names\":\"Shikhar Bansal(bshikhar13131313@gmail.com),\"}","responseJSON":{"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"},"status":200,"statusText":"OK"}]' ;
$data = json_decode($json,1);
echo '<pre>', print_r($data, 1), '</pre>';

and examine the result:

Array
(
    [0] => Array
        (
            [readyState] => 4
            [responseText] => {"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"}
            [responseJSON] => Array
                (
                    [names] => Shikhar Bansal(bshikhar13131313@gmail.com),
                )

            [status] => 200
            [statusText] => OK
        )

)

You should  see that you need

echo $data[0]['responseJSON']['names'];      //--> Shikhar Bansal(bshikhar13131313@gmail.com)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.