How to trim a output and insert it as a variable to at.js

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

,"responseText":"\n\n{\"names\":\"Shikhar Bansal(,\"}"
,"responseJSON":{"names":"Shikhar Bansal(,"}

but i want only the “Shikhar Bansal(” 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

		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: [
		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 = [];
            url: "jsdropdown.php",
            data: {
              "uname": match[1]
            async: false,
            complete: function(res) {
              // results=res;
              // alert(JSON.stringify(res));
              return res;
            dataType: "json"
          var results1=JSON.stringify(results);
            at: "@",
            data: results1

here is a js fiddle:

What you’re wanting is at index 0, with a property of responseJSON

If a variable of res contains the following response:

res = [{"readyState":4
,"responseText":"\n\n{\"names\":\"Shikhar Bansal(,\"}"
,"responseJSON":{"names":"Shikhar Bansal(,"}

Then the data is found at res[0].responseJSON

var data = res[0].responseJSON;

and the username you want is at data.names

var username = data.names; // Shikhar Bansal(,
1 Like

it works now @Paul_Wilkins but what if i want to fetch multiple names will it work also for that. if not then how can i get that multiple names

Then, the information is likel to be comma separated, so you can just split on the comma to get an array of names.

1 Like

since i’m new to javascript i did this but it is not splitting what to do? @Paul_Wilkins

var dat=results[0].responseJSON;
          var string=dat.names;
          var names=new Array();
          var names=string.split(',');

What’s contained in the data.names property?

@Paul_Wilkins sorry i posted the wrong code here is the original code

var dat=results[0].responseJSON;
          var name1=new Array(dat.names);
         var names=name1.split(",");

here is the values inside dat.names from name1 variable


Won’t the parenthesis need to be removed too?

i typed for pointing it like an example @Paul_Wilkins

Are there, or are there not, parenthesis in the names property?

there are no paranthesis @Paul_Wilkins

Thank you.

The following jsfiddle example code might be instructive for you.

var results = [{
    responseJSON: {
        names: "maya.vmk,mmkk,shan2batman,Dineshmangunta,immu,Lavs79,Gvenvidhya,Maddyy,aboutthecreator,Sample,Sarvo12"
var dat = results[0].responseJSON;
var names = dat.names.split(",");
alert("There are " + names.length + " names.");

If there were parenthesis surrounding the names, you could have used the slice method to remove chars from the start and end of the string.

var results = [{
    responseJSON: {
        names: "(maya.vmk,mmkk,shan2batman,Dineshmangunta,immu,Lavs79,Gvenvidhya,Maddyy,aboutthecreator,Sample,Sarvo12)"
var dat = results[0].responseJSON;
var names = dat.names.slice(1, -1).split(",");
alert("The " + names.length + " names are\n" + names.join("\n"));
1 Like

@Paul_Wilkins thanks a ton it works now

@Paul_Wilkins how to mark the thread as solved sir

Normally threads here are left as-is. There’s a 3-month period before the thread is automatically closed, allowing you to reply further on the same thread if there are any other related issues.

1 Like

ok, thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.