Remove comma at the begining at the of new results output - javascript

I use this script to copy values from span class values in a form. When I copy more than one the script adds a comma at the beginning of the returned results. Not sure what the problem is.
<script>
var results = [];
$('input[name="clickme"]').change(function () {
var id = $(this).attr('class');
$('table#' + id).toggleClass('selected');
var PROC_CODE = $('table#' + id + ' .PROC_CODE').text();
var MEDICARE = $('table#' + id + ' .MEDICARE').text();
var STATUS = $('table#' + id + ' .STATUS').text();
var ATA_ID = $('table#' + id + ' .ATA_ID').text();
var SYS_APP = $('table#' + id + ' .SYS_APP').text();
var SUBMITTER = $('table#' + id + ' .SUBMITTER').text();
var EMAIL = $('table#' + id + ' .EMAIL').text();
var ADD_CPT = $('table#' + id + ' .ADD_CPT').text();
var change = $('table#' + id + ' .change').text();
if ($('input.' + id).is(':checked')) {
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
if ('console' in window) console.log(results);
$('#results').val(results);
} else {
results.pop(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
$('#results').val(results);
}
});
</script>

var results = [];
You have declared result as an array.
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
you are pushing elements into array
$('#results').val(results);
results array will be converted into comma seperated string and then it will be put into your #results element
To solve do something like below i.e.
var result=''; //using as string
...
change
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
to
results= results + ' '+ PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n';

Related

How to get JSON result in grid/tabular format while calling an API?

I'm using railway API in my website and want the Train data in grid format. Please help me with the same.
I want all the variables (Train name, Train number, Departure Time, Arrival Time, Travel Time, Availability Status) in a table format. I'm calling two APIs to get the final result. How can I achieve this using AngularJs?
function between(trainData) {
var total = trainData.TotalTrains;
for (i = 0; i < total; i++) {
var source = trainData.Trains[i].Source;
var destination = trainData.Trains[i].Destination;
var name = trainData.Trains[i].TrainName;
var number = trainData.Trains[i].TrainNo;
var ttime = trainData.Trains[i].TravelTime;
var deptime = trainData.Trains[i].DepartureTime;
var arrtime = trainData.Trains[i].ArrivalTime;
$('.' + className + '').append("<br/>" + name + "(" + number + ")" + " " + ttime + " " + deptime + " " + arrtime + "<br/>");
}
}
}
you can append with the in the end like
$('.' + className + '').append("<table><tr><th>name</th><th>number </th><th>ttime </th><th>deptime </th><th>arrtime </th><th>classcode </th><th>status </th><th>jdate </th></tr><tr><td>" + name + "</td><td>" + number + "</td><td>" + ttime + "</td><td>" + deptime + " </td><td>" + arrtime + " </td><td>" + classcode + "</td><td>" + status + "</td><td>" + jdate + "</td></tr></table>");

Putting the variable into the a href tag

i am having issue with assigning the variable into the URL
This is the code
var value = i+1;
var customPopup = 'Latitude: ' + data.Table[i].Latitude + '</br>Longitude: ' + data.Table[i].Longitude
+ '</br>Station: ' + data.Table[i].StationID + ' </br>Box: ' + data.Table[i].BoxID + '</br>Timestamp: ' + data.Table[i].LocationSend + "<br><a target='_blank' href='/Home/History?DeviceID= ' style='color: #000000'>Click Here For Location History</a></br>";
String literals might help (using backticks ``):
var value = i+1;
var customPopup = 'Latitude: ' + data.Table[i].Latitude +
'<br/>Longitude: ' + data.Table[i].Longitude +
'<br/>Station: ' + data.Table[i].StationID +
'<br/>Box: ' + data.Table[i].BoxID +
'<br/>Timestamp: ' + data.Table[i].LocationSend +
`<br/><a target='_blank' href='/Home/History?DeviceID=${value}' style='color: #000000'>Click Here For Location History</a><br/>`;

Uncaught SyntaxError: Unexpected identifier error at a single quote in webview

I am passing certain values to a form which is loaded on webview to autofill it using JavaScript. It works perfectly till any one of the Strings passed has a single quote " ' " in it. When a single quote is encountered I get this error:
Uncaught SyntaxError: Unexpected identifier
And no data gets filled.
The code to autofill the form
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
PageURL = view.getUrl();
PageTitle = view.getTitle();
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(PageTitle);
}
actionBar.setSubtitle(PageURL);
final String js = "javascript: " +
"var nameDoc = document.getElementsByName('name');" +
"nameDoc[0].value = '" + n + "';" +
"var checkOutDoc = document.getElementsByName('checkout');" +
"checkOutDoc[0].value = '" + btn_co + "';" +
"var noOFPaxDoc = document.getElementsByName('no_of_pax');" +
"noOFPaxDoc[0].value = '" + a + "';" + // a should be int based on ur HTML
"var noOFKidDoc = document.getElementsByName('no_of_kid');" +
"noOFKidDoc[0].value = '" + k + "';" + // a should be int based on ur HTML
"var noOFRoomsDoc = document.getElementsByName('no_of_rooms');" +
"noOFRoomsDoc[0].value = '" + r + "';" + // a should be int based on ur HTML
"var checkInDoc = document.getElementsByName('checkin');" +
"checkInDoc[0].value = '" + btn_ci + "';" +
"var email = document.getElementsByName('guest_email');" +
"email[0].value = '" + m + "';" +
"var resortName = document.getElementsByName('resort_name[]');" +
"resortName[0].value = '" + mail_list[0] + "';" +
"var distFrom = document.getElementsByName('distance_from[]');" +
"distFrom[0].value = '" + dist + "';" +
"var roomType = document.getElementsByName('room_category[]');" +
"roomType[0].value = '" + room_list[0] + "';" +
"var price = document.getElementsByName('package_price[]');" +
"price[0].value = '" + room_price_list[0] + "';" +
"var distance = document.getElementsByName('distance[]');" +
"distance[0].value = '" + distance + "';" +
"var ex = document.getElementsByName('excursions[]');" +
"ex[0].value = '" + ex + "';" +
"var act = document.getElementsByName('activities[]');" +
"act[0].value = '" + act_f + "';" +
"var dest = document.getElementsByName('destination');" +
"dest[0].value = '" + state + "';" +
"var days = document.getElementsByName('total_days');" +
"days[0].value = '" + d + "';" +
"javascript:(function(){" +
"l=document.getElementsByName('submit');" +
"e=document.createEvent('HTMLEvents');" +
"e.initEvent('click',true,true);" +
"l[0].dispatchEvent(e);" +
"})()";
if (Build.VERSION.SDK_INT >= 19) {
view.evaluateJavascript(js, new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
}
});
} else {
view.loadUrl(js);
}
}
It happens because your strings are not escaped. When you insert data directly into JS as you're doing the computer doesn't know the difference between cod you entered and code that was added from the input field.
Thus, when your users input some text
nameDoc[0].value = '" + n + "';
becomes
nameDoc[0].value = 'My name is Norbs' and I'm breaking your code';
In the example above, the string is ended after "Norbs", and as what comes afterwards is not valid JS the script fails.
Possible solution
Change
nameDoc[0].value = '" + n + "';
To
nameDoc[0].value = '" + n.replace("'", "\'") + "';.

how to start each function from index 1 to post json objects in different div in html

I have this json data
somehow i displayed this part of data in html
and want to display this part of data
into div in html but i am unable to display it using for loop and don't to how to start each function from index 1 as index 0 in personal details data.
here is my code
var url = "http://localhost/ReadExchange/api.php";
$.getJSON(url, function(data) {
if (data) {
alert("hey got the data" + JSON.stringify(data));
var arr = data.length;
//$.each(data, function(i,element) {
var element = data[0];
$("#postjson").append(
'<div id="' + element.id + '">' + '<p>' + 'FirstName:' + element.FirstName + '<br/>'
+ 'MiddleName:' + element.MiddleName + '<br/>' + 'LastName:' + element.LastName + '<br/>' + 'Gender:' + element.Gender + '<br/>' + 'Location:' + element.Location + '<br/>' + 'Email:' + element.Email + '<br/>' + 'Mobile:' + element.Mobile + '<br/>' + '</p>' + '</div>'
);
for (var i = 1; i < arr; i++) {
$("#postjson").append(
'<div id="">' + '<p>' + 'BookTitle:' + data[i].data.BookTitle + '<br/>' + 'BookGenre:' + data[i].data.BookGenre + '<br/>' + 'BookWriter:' + data[i].data.BookWriter + '<br/>' + 'Gender:' + data[i].data.BookDescription + '<br/>'
+ '</p>' + '</div>'
);
}
} else {
return;
}
You can use Array.prototype.splice()
data.splice(0, 1);
then iterate data array at for loop, with i set to 0
var data = [{abc:123}, {def:456}];
data.splice(0, 1);
for (var i = 0; i < data.length; i++) {
alert(JSON.stringify(data))
}

XRMService Toolkit not matching parent account guid with another guid stored in a single line text field

I've a hidden field on order form that store guid of parent account of selected contact. Now, in following code snippet I am trying to compare an account guid with that stored in hidden field. But it is returning zero length.
function getOrdersDetail(accountId)
{
var query =
"<a:ColumnSet>" +
"<a:AllColumns>false</a:AllColumns>" +
"<a:Columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"<b:string>createdon</b:string>" +
"</a:Columns>" +
"</a:ColumnSet>" +
"<a:Criteria>" +
"<a:Conditions />" +
"<a:FilterOperator>And</a:FilterOperator>" +
"<a:Filters>" +
"<a:FilterExpression>" +
"<a:Conditions>" +
"<a:ConditionExpression>" +
"<a:AttributeName>new_accountid</a:AttributeName>" +
"<a:Operator>Equal</a:Operator>" +
"<a:Values xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"<b:anyType i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>"+ accountId +"</b:anyType>" +
"</a:Values>" +
"</a:ConditionExpression>" +
"</a:Conditions>" +
"<a:FilterOperator>And</a:FilterOperator>" +
"<a:Filters />" +
"</a:FilterExpression>" +
"</a:Filters>" +
"</a:Criteria>" +
"<a:Distinct>false</a:Distinct>" +
"<a:EntityName>salesorder</a:EntityName>" +
"<a:LinkEntities />" +
"<a:Orders/>" +
"<a:PageInfo>" +
"<a:Count>0</a:Count>" +
"<a:PageNumber>0</a:PageNumber>" +
"<a:PagingCookie i:nil='true' />" +
"<a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>" +
"</a:PageInfo>" +
"<a:NoLock>false</a:NoLock>";
var orders = XrmServiceToolkit.Soap.RetrieveMultiple(query);
alert(orders.length);
}

Categories