Escape sequence jQuery for using variable inside quotes - javascript

I am trying to delete a column in table with the following.
var name = "1";
var name1= parseInt($name);
$('td:nth-child(name1)').hide();
This does not do anything. But if i hardcode it as
$('td:nth-child(1)').hide();
It deletes the column. Can anyone please suggest where i am going wrong?

Just concatenate the strings with the variables:
$('td:nth-child(' + name1 + ')').hide();

Javascript does not do string interpolation. So you need to concatenate your strings together like this:
var name = "1";
var name1= parseInt(name); // removed $ before variable name
$('td:nth-child(' + name1 + ')').hide();
Or, without doing converting to int, which isn't really necessary:
var name = "1";
$('td:nth-child(' + name + ')').hide();

Related

How to pass classname to a jquery function through varible

I have a two variable m and b having class names,
m='box1'
b='box2'
I want to assign the values of m and n to jquery function so that it will work
i want it to be like $('.box1') and $('.box2')
I have done the below code but giving me error
which is the correct syntax for entering it?
$('.+m+').toggleClass('flip');
$('.+b+').toggleClass('flip');
You can directly pass the variable inside $().
var m = '.box1';
var b = '.box2';
$(m).toggleClass('flip');
$(b).toggleClass('flip');
If you want to add . seperately, you can concatenate it inside $()
var m = 'box1';
var b = 'box2';
$('.'+m).toggleClass('flip');
$('.'+b).toggleClass('flip');
You should concatenate the variable like:
$('.'+m)
If you want to use both at once:
$('.'+m+', .'+n)
Or if you want to add later:
var m = $('.'+m);
m.add('.'+n);
Try concatenation the class symbol and variable in the following way:
$('.' + m + ', .' + b).toggleClass('flip');
You need to concatenate like so:
$("." + m + ", ." + b).toggleClass("flip");
You could also use ES6 template literals:
$(`.${m}, .${b}`).toggleClass("flip"):

Change data inside string with box brackets

I have a string
garments[0][1]; // The 0 and 1 can be other numbers
I need to replace the data inside the second and the third box brackets.
[0] and [1]
So that it can be
garments[4][6]
Please let me know your suggestions when you get a chance, thank you.
You can try that:
var string = 'garments[' + 4 + '][' + 6 + ']'; //in your onClick function
//To increment dynamically:
var string = 'garments[' + i + '][' + j + ']'; //i and j being variables incrementing in your loops/treatments
Update to address comments:
If you want to break "garnments[0][1]" into "garnments",0 and 1 you can do the following:
var string = "garnments[0][1]";
string = string.split('['); //string = [["garnments"],["0]"],["1]"]]
string[1].replace(']','');
string[2].replace(']',''); //string = [["garnments"],["0"],["1"]]
You can then change values and rebuild your string for further use.
It is a bit brutal though. You can use RegExp as showed by #Diego
You can use String.prototype.replace()
'garments[0][1]'.replace('[0]','[4]').replace('[1]','[6]')
For any possible string with ***[m][n] format:
Function SetNewValues(testString, n, m)
{
var keyWordLengh = testString.indexOf("[");
return testString.substring(0,keyWordLengh) + "[" + n.toString() + "][" + m.toString() + "]";
}
Where:
testString is entire string to work on, like "something[342][345]"
n,m are values to be put inside brackets :)
This would be my approach.
var string = "['foobar'][2][12]";
var match =
/\[([^\]]+)\](?:\[(\d+)\])(?:\[(\d+)\])/g
.exec(string);
console.log(match);

Adding the single string quote for value inside variable using Javascript

I have a variable, before I use that variable,I need to add string quotes to the value/data which is inside the variable using JavaScript need help
//i am getting like this //
var variable=king;
//i want to convert it with single quote//
variable=variable;
but i need the data inside variable should be in a single quotation.
You can concatenate the variable with your quotes like :
function addQuotes(value){
var quotedVar = "\'" + value + "\'";
return quotedVar;
}
And use it like :
var king = addQuotes('king');
console.log will display :
'king'
Edit : you can try it in the chrome/firefox console, I did it and it works perfectly when copy/paste from here.
var x = 'abc';
var sData = "\'" + x +"\'";
// sData will print "'abc'"
var x = 'pqr';
var sData = "\'" + x +"\'";
// sData will print "'abc'"
1) You can use doublequotes
var variable = "'" + variable + "'";
2) ... or You can escape single quote symbol with backslash
var variable = '\'' + variable + '\'';

Passing handlebars variable to javascript function or JQuery function?

Is it possible to pass handlebars variable to javascript function or JQuery function ?
I am trying to something like this
$(document).ready(function(){
var first = {{name}}
var last = {{last}}
$("#name").text(first + ' ' + last);
})
I assume that name and last are string.
Then use this code:
$(document).ready(function(){
var first = "{{name}}";
var last = "{{last}}";
$("#name").text(first + ' ' + last);
})
But be sure to escape the " and newlines in your name and last vars.

how to Remove the characters in between a string using index in jquery or javascript?

I want to remove the few characters in a string using index.
for example:
My input is: "5,4,3,2,1"
I want to remove the 1th to 2nd index position characters(here , to 4).
The Output should be 5,3,2,1.
Is there any predefined function in jquery or javascript to done this?
You can try to use substring function like this:
var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));
JSFIDDLE DEMO
I would juse use javascripts split function for that.
So if you have
var string = "5,4,3,2,1";
than you just need to do
var splitted = string.split(",");
whereas the character in the brackets is the one you want to split on. After you did that, you can just make a new string, and build it with the array elements.
So you do something like
var string2 = splitted[0] + "," + splitted[2] + "," + splitted[3] + "," splitted[4];

Categories

Resources