hi there
i wanted to put this:
document.write("y = " + i);
inside another variable, like this:
s=s+"\"y = \" \+ i";
but now, i variable is not variable, just a plain text.
how can i fix this?
output would be
document.getElementById("output").innerHTML=s;
thank you
You do not pass to s string a variable - just text i. Try with:
s=s+"y = " + i;
You can also convert it to:
s += "y = " + i;
Related
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"):
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);
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 + '\'';
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();
I have a javascript string that contains the following:
data-href="/Admin/Edit?pk=0001I&rk=50050055"
How can I change this string so the value of rk is changed to a value held in the string newRowKey? Not sure if it helps but the data format always looks like this with the rk followed by an = and then terminated with the "
datahref="/Admin/Edit?pk=0001I&rk=50050055";
to change it if your building the URL
datahref="/Admin/Edit?pk=0001I&rk="+ newRowKey;
or replace it if you know the existing value
datahref.replace("50050055", newRowKey);
if you do not know the value of rk, but you know its last in the URL, you could use indexOf to find it.
datahref = datahref.substring(0,datahref.indexOf("rk=")+3) + newRowKey;
Maybe you want a regular expression to replace that rk.
newRowKey = 'XXXXXX'
s = 'Edit?pk=0001I&rk=50050055';
s2 = s.replace(/(.*;)(rk=)(.*)(&|)/, '$1$2' + newRowKey)
alert( s + "\n" + s2 );
//2nd test
s = 'Edit?pk=0001I&rk=50050055&';
s2 = s.replace(/(.*;)(rk=)(.*)(&|)/, '$1$2' + newRowKey)
alert( s + "\n" + s2 );
​demo: http://jsfiddle.net/fedmich/g3TjE/
data_href.replace(/rk=.*$/,'rk=' + newRowKey);