I have column in my database named rate but the datatype is string and has values like '2000+'. When I try to show this value using jQuery, why is it only showing 2000 without the '+' sign?
for (var j = 0; j < dtt2.length; j++) { if (dt0[i].CategoryID === dtt2[j].CategoryID) { var rate = dtt2[j].Rate; alert(rate.tostring()); $("#" + tbl).append('<tr><td>' + dtt2[j].ServiceName + '</td><td width="20%">₹ ' + rate.tostring() + '</td></tr>'); } }
this code is working when running on local host but when i am hosting on production the error comes string without + sign
Below is the code behind code-
foreach (DataRow dr in dt2.Rows)
{
ServiceRateList sd = new ServiceRateList();
sd.ServiceDetailID = Convert.ToInt32(dr["ServiceDetailID"]);
sd.ServiceName = dr["ServiceName"].ToString();
sd.CategoryID = Convert.ToInt32(dr["CategoryID"].ToString());
sd.CategoryName = dr["CategoryName"].ToString();
sd.Rate = dr["rate"].ToString();
bislist.Add(sd);
}
Note sure how you displaing plus sign in your JS codes.
But here work-around is to escape the plus sign by preceding it with a backslash.
var data = dbrawdata.replace('+', '\\+');
It would be good to answer your question add some line of your codes.
As i understood
Very simple,
var value='2000+';
value.toString(); // => '2000+'
OR
String('2000+'); // '2000+'
Related
I'm trying to make a website that gathers information from APIs. The following code always evaluates to 'Beep Boop Beep! I can\t find the Wikipedia page with the API! :-( \n Anyways here is more info on...'! Anyone have any ideas why?
var geoNamesWiki = result.geoNamesWiki;
for (let j = 0; j < 30; j++) {
if (geoNamesWiki.geonames[j].feature == 'country' &&
(geoNamesWiki.geonames[j].countryCode == openCage.results[0].components["ISO_3166-1_alpha-2"] ||
geoNamesWiki.geonames[j].title.includes(openCage.results[0].components.country))) {
$('#summary').html(geoNamesWiki.geonames[j].summary);
$('#wikiLink').html(geoNamesWiki.geonames[j].wikipediaUrl).attr("href", "https://" + geoNamesWiki.geonames[j].wikipediaUrl);
} else {
$('#summary').html('Beep Boop Beep! I can\t find the wikipedia page with the API! :-( \n Anyways here is more info on' + openCage.results[0].components.country + ':');
$('#wikiLink').html('https://en.wikipedia.org/wiki/' + encodeURI(openCage.results[0].components.country)).attr("href", 'https://en.wikipedia.org/wiki/' + encodeURI(openCage.results[0].components.country));
}
}
Is suspect you have a string there at var geoNamesWiki = result.geoNamesWiki;
Try parsing it to a JSON object first var geoNamesWiki = JSON.parse( result.geoNamesWiki );
I found the answer thanks to #Bekim Bacaj! I was overwriting what I had already done, so just needed to add a break on the final line of the IF part.
I have dropDown element which takes the options in the format
ctrlOptions:{0:'String',1:'int'}
in addition to simple data types i have user defined data types hence i want to populate this dynamicaly . so i used a loop and concatenation
var dropDown = "{"
for(var i=0;i<dataTypesList.length;i++){
if(i == dataTypesList.length-1){
dropDown = dropDown + i + ":" + "'" + dataTypesList[i].Name + "'}";
}else{
dropDown = dropDown + i + ":" + "'" + dataTypesList[i].Name+ "'" + ",";
}}
This yields be the options in required format but along with quotes around it like
ctrlOptions:"{0:'String',1:'int'}"
i want to remove the double quotes i tried with replace it diesnt seem to help. how can i achieve this can i use any other way.
What you want to create is an object & not a string.
So wildly guessing from your code, that the input dataTypesList looks something like this:
dataTypesList = [{Name:'String'}, {Name:'int'}]
You should use :
var dropDown = {};
for(var i=0;i<dataTypesList.length;i++)
dropDown[i] = dataTypesList[i].Name;
And then Output is an object :
{0: "String", 1: "int"}
you can use JSON.parse to convert the options string from string json to object json:
Using your code (slightly modified):
var dropDown = "{"
for(var i = 0; i < dataTypesList.length; i++)
{
if(i === dataTypesList.length - 1)
{
dropDown += i + ":'" + dataTypesList[i].Name + "'}";
}
else
{
dropDown += i + ":'" + dataTypesList[i].Name + "',";
}
}
// later...
ctrlOptions: JSON.parse(dropDown);
Verify your target browsers are compatible: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
If not, there are some libraries compatible with older browsers that do the same thing. JSON2 is recommended by the author for out-of-date browsers: https://github.com/douglascrockford/JSON-js
I'm having trouble with JS .split() method in a GAS script. I copy and paste the headers of other google sheet as variable headers (the usual way will be copy and paste). This pasted selection contains some empty and undefined elements. I need to turn this elements in an array. So I split it and use the .filterto clean empty elements. But, when I run the script, the var arrayHeaders remains equal to headers, as if the .split(" ") didn't make any change, this way:
This is my code:
var headers = "STUDENT Parentage GRADE YEAR DATE GUIDE";
var arrayHeaders = headers.split(" ");
// arrayHeaders = arrayHeaders.filter(function(n){return n});
Logger.log("headers = " + headers);
Logger.log("arrayHeaders = " + arrayHeaders);
Logger.log("arrayHeaders length " + arrayHeaders.length);
for (var i = 0; i < arrayHeaders.length; i++){
var NOME_Cell = sheet.getRange(1, i +1);
Logger.log("NOME_Cell" + NOME_Cell);
Logger.log("arrayHeaders[i]" + arrayHeaders[i]);
NOME_Cell.setValue(arrayHeaders[i]).setBackgroundRGB(34, 139, 34).setFontSize(font_size).setFontWeight("bold").setFontFamily("Arial");
}
If I delete all spaces after paste the string and press space again, then the .split(" ") works well.
Before asking this question, I read this other one, but I'm still stucked with what is going wrong here.
Thanks in advance for any help!
I'm pretty sure your headers variable is not a String. Try this:
var arrayHeaders = headers.toString().split(/[\s\t]+/);
When you are performing split(""), make sure the words in the string are seperated by samething which are passing to .split() method.
Your arrayHeaders is same as the headers.
var headers = "STUDENT Parentage GRADE YEAR DATE GUIDE";
var arrayHeaders = headers.split(" ");
// arrayHeaders = arrayHeaders.filter(function(n){return n});
Logger.log("headers = " + headers);
Logger.log("arrayHeaders = " + arrayHeaders);
Logger.log("arrayHeaders length " + arrayHeaders.length);
for (var i = 0; i < arrayHeaders.length; i++){
var NOME_Cell = sheet.getRange(1, i +1);
Logger.log("NOME_Cell" + NOME_Cell);
Logger.log("arrayHeaders[i]" + arrayHeaders[i]);
NOME_Cell.setValue(arrayHeaders[i]).setBackgroundRGB(34, 139, 34).setFontSize(font_size).setFontWeight("bold").setFontFamily("Arial");
}
Here i think, the seperation of header is not same. make sure the seperation of headers are of one space or two spaces or three spaces..
Using javascript prompt I get two parameters like, search string and search keyword, then search for the keyword and get the number of items found. Then need to show them on the page. Seems to mistake I have made.
<html>
<head>
<script type = "text/javascript">
var counter = 0;
var enter = prompt("Enter your String:");
var search = prompt("Enter words to search:");
var b = search.length;
var a = enter.length - search.length;
for (var y = 0; y <= a; y++)
{
if(b <= enter.length){
if(enter.substring(y,B))
{
counter = counter + 1;
}
b++;
}
else{
document.write("<p>" + "ERROR" + "</p>");
}
document.write("<p>" + "your word:" + enter + "</p>");
document.write("<p>" + "word use:" + counter + "</p>");
</script>
<body>
</body>
</head>
</html>
You are using a variable that is not declared. Remember that Javascript is case sensitive and the variable b is different than B.
You forget to close the for brackets.
You need to compare the substring with the search pattern.
As Sameera Thilakasiri has rightly pointed out, your code is sloppy. If that works for you, great (I suppose), but other people who look at your code may have a hard time following it. Further, sloppy code leads to mistakes that would otherwise be easily caught.
For instance, you have not closed the for loop, which would easily be seen in nicely formatted code.
Beyond the open for loop, the only other problem I see (syntactically) is that JavaScript is a case-sensitive language, which means that b is different from B, which is why your script throws the 'Uncaught ReferenceError: B is not defined' on the line if (enter.substring(y,B)) {.
Try closing your for loop and using a lowercase b on the offending line. Once that is done, you only have to fix the logic errors.
Happy coding.
You can also use indexOf:
var counter = 0;
var enter = prompt("Enter your String:");
var search = prompt("Enter words to search:");
var start = 0;
while(1){
start = enter.indexOf(search,start);
if(start==-1) break;//if nothing found
start++;//next start = current occurrence + 1
counter++;
}
document.write("<p>" + "your word:" + enter + "</p>");
document.write("<p>" + "word use:" + counter + "</p>");
This is the script that i am using to fetch a particular cookie lastvisit :
AFTER THE EDIT
// This document writes a cookie
// called from index.php
window.onload = makeLastVisitCookie;
function makeLastVisitCookie() {
var now = new Date();
var last = new Date();
now.setFullYear(2020);
// set the cookie
document.cookie = "lastvisit=" + last.toDateString() + ";path=/;expires=" + now.toGMTString();
var allCookies = document.cookie.split(";");
for( var i=0 ; i < allCookies.length ; i++ ) {
if(allCookies[i].split("=")[0]== "lastvisit") {
document.getElementById("last_visit").innerHTML = "You visited this site on" + allCookies[i].split("=")[1];
} else {
alert("testing..testing..");
}
}
}
From this script the if part never works though there are 5 cookies stored from my website. (including the cookie that i am saving from this script) What is the mistake that i am making while fetching the cookie named lastvisit ?
You're splitting the cookie by ; an comparing those tokens with lastvisit. You need to split such a token by = first. allCookies[i] looks like key=val and will never equal lastvisit. Een if allCookies[i] == "lastvisit" is true, the result will still not be as expected since you're showing the value of allCookies[i + 1] which would be this=the_cookie_after_lastvisit.
if(allCookies[i].split("=") == "lastvisit") { should be:
var pair = allCookies[i].split("=", 2);
if (pair[0].replace(/^ +/, "") == "lastvisit") {
"You visited this site on" + allCookies[i+1]; should be:
"You visited this site on" + pair[1];
The 2 argument of split makes cookies like sum=1+1=2 be read correctly. When splitting cookies by ;, the key may contain a leading space which much be removed before comparing. (/^ +/ is a regular expression where ^ matches the beginning of a string and + one or more spaces.)
Alternatively, compare it directly against a RE for matching the optional spaces as well (* matches zero or more occurences of a space character, $ matches the end of a string):
if (/^ *lastvisit$/.test(pair[0])) {
I've tested several ways to get a cookie including using regular expressions and the below was the most correct one with best performance:
function getCookie(name) {
var cookie = "; " + document.cookie + ";";
var search = "; " + encodeURIComponent(name) + "=";
var value_start = cookie.indexOf(search);
if (value_start == -1) return "";
value_start += search.length;
var value_end = cookie.indexOf(';', value_start);
return decodeURIComponent(cookie.substring(value_start, value_end))
}
You need to remove possible white space around the cookie key before comparing to the string "lastvisit". This is done conveniently using regular expressions. /^\s+/ matches all white space at the beginning, /\s+$/ matches all white space at the end. The matches are replaced by the empty string, i.e. removed:
for( var i = 0 ; i < allCookies.length ; i++ ) {
var c = allCookies[i].split("="); // split only once
var key = c[0].replace(/^\s+/, '').replace (/\s+$/, ''); // remove blanks around key
if (key == "lastvisit") {
document.getElementById("last_visit").innerHTML = "You visited on " + c[1];
}
//...
}