How to pass classname to a jquery function through varible - javascript

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"):

Related

Adding quotes to a comma separated string

What I have:
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
What I need:
var a = '1.1.1.1','2.2.2.2','3.3.3.3'
What I'm trying:
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
var b = a.split(",")
var c
for (var i=0;i<b.length; i++)
{
c.concat("\'").concat(b[i]).concat("\',\"")
}
What I'm actually getting with the above
"'1.1.1.1','"
I'm only able to get the first element right, how do I rectify this?
Also, in JS, is it even possible to have something like '1.1.1.1','2.2.2.2','3.3.3.3' stored in a variable?
A background to this problem:
I have an iframe whose source is a kibana query. The query in fact takes in values to a particular parameter in the above mentioned format.
Eg:
params:!('1.1.1.1','2.2.2.2')
While my db contains the param values as a string of CSV.
Eg.
"1.1.1.1,2.2.2.2,3.3.3.3"
Try this
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = "'" + a.split( "," ).join( "','" ) + "'";
console.log( b );
You don't need to deal with iterations for this, use a RegExp replace:
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = "'" + a.replace(/,/g, "','") + "'";
console.log( b );
The naive solution to your problem looks like this:
> line = '1.1.1.1,2.2.2.2,3.3.3.3'
'1.1.1.1,2.2.2.2,3.3.3.3'
> '"' + line.replace(/,/g, '","') + '"'
'"1.1.1.1","2.2.2.2","3.3.3.3"'
or if the quotes need to be reversed:
> "'" + line.replace(/,/g, "','") + "'"
'\'1.1.1.1\',\'2.2.2.2\',\'3.3.3.3\''
However, it sounds like what you need is a full-blown CSV parser, to handle cases in which you have quotes and commas and new lines and other crazy characters embedded in your input.
The naive solution seems to be in line, though, with what you were trying to do, and might illustrate why your approach fell short.
Your code works as you intended. Can you append to c without declaring?
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
var b = a.split(",")
var c = ""
for (var i=0;i<b.length; b++)
{
c.concat("\'").concat(b[i]).concat("\',\"")
console.log(b)
}
You can store several values in a variables by using array for example.
If you want to get string like '"1.1.1.1","2.2.2.2","3.3.3.3"' you can use the following code:
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = a.split(',').map(function (str) {
return '"' + str+ '"';
}).join(',');
console.log(b);

Javascript-Node.Js reaching dynamic body elements

I have HTML input elements with names like A[1], A[2], A[3]. I want to catch them in express this way:
var optcount = i + 1;
var columnA=req.body['A['+optcount+']'];
However it does not work.
If I have names like A1, then this works:
var columnA=req.body['A'+optcount];
Any suggestions?
For a quick hack,
var optcount = i + 1;
var prop = 'A[' + optcount + ']';
var columnA=req.body[prop];
Should work.
You should be escaping characters to have a better optimized solution, look at these -> Is there a RegExp.escape function in Javascript? and How to escape regular expression in javascript?

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 + '\'';

Escape sequence jQuery for using variable inside quotes

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();

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