This question already has answers here:
jQuery - how to check if an element exists?
(7 answers)
Closed 6 years ago.
how to check if jQuery .each() function cant find something, then change txt value to 'default'.
$(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active').each(function(i) {
var val = $(this).val();
if (i == 0) {
txt = val;
} else {
txt = txt + ', ' + val;
}
});
im already try
if (i == null) {
txt = 'default';
}
but it doesnt work
Use .length in jquery to get the length
var lnt = $(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active');
if(lnt.length > 0) {
lnt.each(function(i) {
var val = $(this).val();
if (i == 0) {
txt = val;
} else {
txt = txt + ', ' + val;
}
});
}
make use of .length and check its empty or not.
var objcollection = $(this).closest('.filter-select__dropdown-inner').
find('> .button--checkbox.button--active');
if(objcollection.length==0)
{
}
else
{
}
Related
How to increment my global variable 'currentstep' in Google App Script. The third if statement, I used currentstep++ but it doesn't increase as it stayed at 2. Furthermore, I had tried currentstep += 1; and currentstep = currentstep + 1; Both methods don't work as well.
function check_command(data){
var text = data.message.text;
if(text == "/start" || text == "/start"){
currentstep = 1;
return;
}
if (text == "/survey" || text == "/survey"){
currentstep = 2;
return;
}
if (text){
currentstep++;
}
return;
}
In google apps script every time you make a function call the global variable get reinitialized. So typically it's better to use PropertiesService or CacheService for global parameters that you wish to change from one execution to another.
The conditions like this:
if (text == "/start" || text == "/start") { ... }
Have little sense. They are equal with this:
if (text == "/start") { ... }
So your function can be boiled down to this:
function check_command(data) {
var text = data.message.text;
if (text == "/start") { currentstep = 1; return }
if (text == "/survey") { currentstep = 2; return }
if (text) { currentstep++ }
}
And it works fine by itself, as far as I can tell.
Here is a test:
function check_command(data) {
var text = data.message.text;
if (text == "/start") { currentstep = 1; return }
if (text == "/survey") { currentstep = 2; return }
if (text) { currentstep++ }
}
var currentstep = 0;
var data = {message: {text: ""}};
var texts = [
,
"",
false,
"/start",
"/survey",
"",
,
"aaa",
"/survey",
123,
"/start",
""
]
for (let txt of texts) {
data.message.text = txt;
check_command(data);
console.log("currentstep = " + currentstep + " for '" + txt + "'");
}
Probably #Cooper is right. You're doing something fancy that we can't know from your question. Are you running the script several times and trying to keep the global value between the runs?
Update
If you suspect that the problem is a global variable you can modify your function to avoid using the global variable within the function:
function check_command(data, counter) {
var text = data.message.text;
if (text == "/start") return = 1;
if (text == "/survey") return = 2;
if (text) counter++;
return counter;
}
And call the function this way:
currentstep = check_command(data, currentstep);
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 6 years ago.
How to create comma separated amount value in javascript?
For example, if I provide 123000.00 in textbox, the result should be 1,23,000.00
I tried the below function
function formatWithCommasAndDecimal(tempInputString,setvalue,decimalPointLen)
{
var inputString = "";
var beforeDecimal;
var afterDecimal = "00";
var newBeforeDecimal = "";
var commaCounter = 0;
if(tempInputString=="")
{
if(decimalPointLen=="8")
{
document.getElementById(setvalue).value="0.00000000";
}
else
{
document.getElementById(setvalue).value="0.00";
}
return false;
}
if(tempInputString!="0.00" || tempInputString!=" ")
{
if(tempInputString.indexOf(",")!='-1')
{
//Just remove all the commas
var len = tempInputString.length
for(i=0;i<len;i++)
{
if(tempInputString.charAt(i) != ",")
{
inputString = inputString + tempInputString.charAt(i);
}
}
}
else
{
tempInputString=parseFloat(tempInputString).toFixed(decimalPointLen);
inputString = tempInputString;
}
var tempArr = inputString.split(".");
//Format the portion before the decimal
beforeDecimal = tempArr[0];
for(i = beforeDecimal.length - 1;i>=0;i--)
{
newBeforeDecimal = newBeforeDecimal + beforeDecimal.charAt(i);
commaCounter++;
if(commaCounter % 3 == 0)
{
//Checking this so that the comma is not appended at the end
if(i > 0)
{
newBeforeDecimal = newBeforeDecimal + ",";
commaCounter = 0;
}
}
}
beforeDecimal = "";
for(i=newBeforeDecimal.length-1;i>=0;i--)
{
beforeDecimal = beforeDecimal + newBeforeDecimal.charAt(i);
}
//Format the portion after the decimal (if any is there)
if(tempArr.length != 1)
{
afterDecimal = tempArr[1];
}
var finalString = beforeDecimal + "." + afterDecimal;
document.getElementById(setvalue).value=finalString;
}
return finalString;
}
When i entered only one email id, and check in inspecter, it shows like this
I need to remove commas,if i enter only one id and it need to show commas, if i enter one more or multiple id's given it shows comma. Can you please help ?
function GetTextValue() {
$(divValue).empty();
$(divValue).remove(); values = '';
values += this.value + ','
});
document.all.contact_list.value = values;
}
You can do it like that.
function GetTextValue() {
$(divValue).empty();
$(divValue).remove(); values = '';
$('.input').each(function() {
divValue = $(document.createElement('div')).css({
padding:'5px', width:'200px'
});
if (this.value == '') {
// alert('Empty');
} else if (values != '') {
values += ',';
}
values += this.value;
});
document.all.contact_list.value = values;
}
JSFiddle
Hope it will be useful for you.
function GetTextValue() {
$(divValue).empty();
$(divValue).remove();
var values = '';
$('.input').each(function() {
divValue = $(document.createElement('div')).css({
padding:'5px', width:'200px'
});
if(this.value.trim() != ''){
if(values != ''){
values += ',';
}
values += this.value.trim();
}
});
document.all.contact_list.value = values;
}
function GetTextValue() {
$(divValue).empty();
$(divValue).remove();
var values = '';
$('.input').each(function() {
divValue = $(document.createElement('div')).css({padding:'5px', width:'200px'});
if(this.value != '') {
values += this.value;
values += ',';
}
});
document.all.contact_list.value = value.substring(0, value.length - 1);//remove last comma
}
This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 8 years ago.
function ShowEditBox(serial)
{
$("#divEditBox").slideDown("medium");
var pserial ='PName'+ serial;
var colindex = 0;
var $tr = $("#" + pserial).parent().parent();
$tr.find('td').each(function () {
if (colindex == 2) {
$("#txtName").val($(this).text());
} else if (colindex == 3) {
$("#txtSurName").val($(this).text());
} else if (colindex == 4) {
$("#txtEmail").val($(this).text());
} else if (colindex == 5) {
$("#txtMobile").val($(this).text());
} else if (colindex == 6) {
$("#txtAddress").val($(this).text());
}
colindex++;
})
$("#hdField").val(serial);
}
Whenever i click the edit button in grid view that particular row data should be displayed in text boxes. But here i am getting unnecessary spaces in text boxes.
How can i trim the spaces in the Text box(txtName) ?? I am getting spaces in text .
Try to use $.trim("string"),
$("#txtName").val($.trim($(this).text()));
use trim() function of javascript
for jquery look at here Trim
Look at here
example
var str = " lots of spaces before and after ";
$( "#original" ).html( "Original String: '" + str + "'" );
$( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
Use javascript trim() method.
$("#txtName").val($(this).text().trim());
function ShowEditBox(serial)
{
$("#divEditBox").slideDown("medium");
var pserial ='PName'+ serial;
var colindex = 0;
var $tr = $("#" + pserial).parent().parent();
$tr.find('td').each(function () {
if (colindex == 2) {
$("#txtName").val($.trim($(this).text()));
} else if (colindex == 3) {
$("#txtSurName").val($.trim($(this).text()));
} else if (colindex == 4) {
$("#txtEmail").val($.trim($(this).text()));
} else if (colindex == 5) {
$("#txtMobile").val($.trim($(this).text()));
} else if (colindex == 6) {
$("#txtAddress").val($.trim($(this).text()));
}
colindex++;
})
$("#hdField").val(serial);
}
I am trying to get it so that if I type in a name that ends with a space, the textfield will go red. Most of the code works its just one method does not seem to be working.
The issue must be somewhere in the last index part?
var NamePass = true;
function ValidateName() {
var BlankPass = true;
var GreaterThan6Pass = true;
var FirstBlankPass = true;
var BlankMiddleName = true;
if (document.getElementById('Name').value == "") {
BlankPass = false;
}
var Size = document.getElementById('Name').value.length;
console.log("Size = " + Size);
if (Size < 7) {
GreaterThan6Pass = false;
}
if (document.getElementById('Name').value.substring(0, 1) == " ") {
FirstBlankPass = false;
}
var LastIndex = document.getElementById('Name').value.lastIndexOf();
if (document.getElementById('Name').value.substring((LastIndex - 1), 1) == " ") {
FirstBlankPass = false;
}
string = document.getElementById('Name').value;
chars = string.split(' ');
if (chars.length > 1) {} else
BlankMiddleName = false;
if (BlankPass == false || GreaterThan6Pass == false || FirstBlankPass == false || BlankMiddleName == false) {
console.log("BlankPass = " + BlankPass);
console.log("GreaterThan6Pass = " + GreaterThan6Pass);
console.log("FirstBlankPass = " + FirstBlankPass);
console.log("BlankMiddleName = " + BlankMiddleName);
NamePass = false;
document.getElementById('Name').style.background = "red";
} else {
document.getElementById('Name').style.background = "white";
}
}
http://jsfiddle.net/UTtxA/10/
lastIndexOf gets the last index of a character, not the last index in a string. I think you meant to use length instead:
var lastIndex = document.getElementById('Name').value.length;
Another problem with that, though, is that substring takes a start and end index, not a start index and a substring length. You could use substr instead, but charAt is easier:
if (document.getElementById('Name').value.charAt(lastIndex - 1) == " ") {
FirstBlankPass = false;
}
Now, for some general code improvement. Instead of starting with all your variables at true and conditionally setting them to false, just set them to the condition:
var NamePass = true;
function ValidateName() {
var value = document.getElementById('Name').value;
var BlankPass = value == "";
var GreaterThan6Pass = value.length > 6;
var FirstBlankPass = value.charAt(0) == " ";
var LastBlankPass = value.charAt(value.length - 1) == " ";
var BlankMiddleName = value.split(" ").length <= 1;
if (BlankPass || GreaterThan6Pass || FirstBlankPass || LastBlankPass || BlankMiddleName) {
console.log("BlankPass = " + BlankPass);
console.log("GreaterThan6Pass = " + GreaterThan6Pass);
console.log("FirstBlankPass = " + FirstBlankPass);
console.log("BlankMiddleName = " + BlankMiddleName);
NamePass = false;
document.getElementById('Name').style.background = "red";
} else {
document.getElementById('Name').style.background = "white";
}
}
A couple more points of note:
It’s probably a good idea to use camelCase variable names instead of PascalCase ones, the latter usually being reserved for constructors
blah == false should really be written as !blah
An empty if followed by an else can also be replaced with if (!someCondition)
That function looks like it should return true or false, not set the global variable NamePass
Penultimately, you can sum this all up in one regular expression, but if you intend to provide more specific error messages to the user based on what’s actually wrong, then I wouldn’t do that.
function validateName() {
return /^(?=.{6})(\S+(\s|$)){2,}$/.test(document.getElementById('name').value);
}
And finally — please keep in mind that not everyone has a middle name, or even a name longer than 6 characters, as #poke points out.