I can get the variable I created to work but not with the variable "+number+" in it
This is what I have so far:
//this works when i remove "+number+"
var test = "text row 1 \n\n text row 2 \n\n number "+number+" text row3"
$(document).mousemove(function(event){
var result = confirm(test);
});
$(document).ready(function(){
alert(test);
});
how do I get this to work?
Update:
number is defined in an external script that is included into the page. It's not my javascript so I don't know how it's defined, I just need a way make the variable work with number in it
the variable number is not defined in the given code. It has to be defined first.
var number = 0; // for example
if number is defined somewhere else (that you cannot make sure about it), then here is a suggestion
number = (typeof(number)=='undefined')? 0 : number ;
var test = "text row 1 \n\n text row 2 \n\n number "+number+" text row3"
The previous code would evaluate (examine) first the value of number, if it is not defined before, it will assign the value 0 for it, otherwise will return the value of number as required.
Hope this helps :-)
Related
last week I asked something here Increment ID from last row google apps script
Everything was working well but then again, when I used WP1-1000 as a starting row, the result is still appearing as WP1-0NaN
var riskid = mysheet.getRange(rlast,2).getValue();
if (riskid.length > 3){
// Extract number ex. 3
var riskidnb = parseInt(riskid.substring(1,riskid.length));
// Increase risk number +1
riskidnb++
// Convert to string "0004"
var s = "000" + riskidnb.toString();
// Write risk nb i.e. "R004"
mysheet.getRange(r,2).setValue("WP1-"+ s.substring(s.length-4))
}
I tried changing/increasing/decreasing the riskid.length, var s, and s.length-4 from the code but still no avail. The result still appears as "WP1-0NaN"
From my question, the string is already inverted into an integer, but it still appears as NaN when I changed it to WP1.
Also, it seems the code from my last question only workds if there is only 1 letter like in the solution.
I literally tried everything for 2 hours and going mad now.
Explanation / Issue:
That is because in your previous question, the id has the structure
of R-002 but now you are using 3 letters before the -:
WP1-1000. You can now use 4 instead of 1 and it will work:
parseInt(riskid.substring(4,riskid.length));
However, a more generic approach would be to substring after -, therefore you can use indexOf to find that position:
parseInt(riskid.substring(riskid.indexOf('-')+1,riskid.length));
You can apply the same logic for the last line. Instead of hardcopying WP1- you can just get the text before and including -:
riskid.substring(0,riskid.indexOf('-')+1);
Solution:
var riskid = mysheet.getRange(rlast,2).getValue();
if (riskid.length > 3){
// Extract number ex. 3
var riskidnb = parseInt(riskid.substring(riskid.indexOf('-')+1,riskid.length));
// Increase risk number +1
riskidnb++
// Convert to string "0004"
var s = "000" + riskidnb.toString();
// Write risk nb i.e. "R004"
var start = riskid.substring(0,riskid.indexOf('-')+1);
mysheet.getRange(r,2).setValue(start + s.substring(s.length-4))
}
I have two buttons and there is a form in between. One button says "+" and the other "-". I am trying to make increment/decrement buttons, but it is not properly working.
It seems like addition is causing a problem where increment is not happening, and instead, concatenation is happening. For example, when the form value is 0, and "+" button is pressed, it changes the 0 to 01.
My JavaScript code has
//assume the oldValue read 0 from the form
newValue = oldValue + 1
alert(newValue); //this returns 01 instead of 1
When the oldValue is 01 and increment again, it returns 011. Why is this not incrementing, but concatenating the 1 at the end?
Surprisingly, decrement is working perfectly with the same code except that I have a minus where there is a plus.
How can you increment a form value in peace? and can anyone explain why "+ 1" does not work?
newValue = Number(oldValue) + 1
I think you forgot to convert string to int.
Try this:
newValue = parseInt(oldValue) + 1;
This is happening because oldValue datatype is string and when you add a Int to a String then the output is also a string. So you have to convert it into a number before adding some int value into it.
CASE 1:
var a=1;
var b=2;
alert(a+b);// RESULT WILL BE 3
CASE 2:
var a='1';
var b=2;
alert(a+b);// RESULT WILL BE 12
If you are getting values from a Form or something , your value will
be treated as a string (like '1') . You can convert if to number/int
by Using the Keyword Number , Like below
alert(Number(a)+b);
I am making a game where in you click on three circles, with their ids as one, two and three, and a variable which gets a random number every onClick. If your choice on the circle and the random number matches, you get one point. I want to keep track of score. I made a variable scor, and put it to += 1, but it just appends it (011111). How can i fix this?
if(image === hidd){
document.getElementById("abc").innerHTML="ITS CORRECT!!!";
aaa.src="http://www.clker.com/cliparts/3/v/I/F/6/V/light-blue-circle-md.png";
var scor=document.getElementById("score").innerHTML
scor = scor + 1;
document.getElementById("score").innerHTML=scor;
}
else{
document.getElementById("abc").innerHTML="Try Again";
}
}
NOTE: Image variable is the choice, and hidd is the random number. abc will tell the user whether he hit the correct circle or not, and aaa is the correct option. id score is the h1 heading to display score. Please tell me wats wrong.
Thanks
Aaryamann
Convert your scor to a numeric value by placing a Unary Plus (+) operator in front of the innerHTML data you assign to it:
var scor = +document.getElementById("score").innerHTML;
^
Instead of scor + 1 to increment your value, you can use one of the following:
scor += 1; // or...
++scor; // or...
scor++;
i have a table like below which each content is input pop-up
on square i spot you see on last digit have 1,2,8,9 format.
in my html the content of table is value of Posisi
Nomor Rak
<br><input type="text" id="posisi" readonly/></br>
that automaticly i pick using
<td class="data206"><div align="center"><input class="data206" type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this});setvalue(this.value);" value="A1.7.8" /></div></td>
for an example.
so my pop-up like this
my purpose to bind the last digit and then i can manipulate line value. So for an idea script will like below
$(document).ready(function(){
var posisi = $("#posisi").val();
if (posisi== like "1" or like "2" or like "8" or like "9" ){
$("#line").val("9")
}
});
My problem: I don't know how to bind the last digit in jquery..to make conditonal that $("#posisi") last digit value between 1 or 2 or 8 or 9. if this conditional true i can add value in $("#line").val("whatever i want")
need help with an example will be appreciate
Your var posisi will contain a string like "A1.7.8" and you can easily get the last character of string. This may help
How can I get last characters of a string using JavaScript
There are a few more tips which may help.
It seems that you do not want to wrap the code in document.ready. I think you want to get the last digit value on every click. so get the value in "popup_window_show" function or function you are using to show popup.
Moreover if you want to make calculation on that number i.e 1,2,8,9 then convert it into integer form first.
var posisi = $("#posisi").val();
var regex = /\.(\d+)\.?$/;
// Catches the last set of digits following a period.
// Allowing for an additional period at the end.
var matches = posisi.match(regex);
if (matches) {
var lastNumber = parseInt(matches[1], 10);
// Do something
$("#line").val(lastNumber);
}
I found this code on another question, but am not sure how to use it:
var text = document.getElementById("text").innerHTML;
text = text.replace(/"z"/g,
function(n){ return ++n });
document.write(text);
It's exactly what I need, but I'm not sure how to pass a number to the function. Essentially, I have a large list of csv (comma delimited) items and need to look for each occurrence of "z" and replace it with a number incremented by one. The code is here, but I'm just not knowledgeable yet to know how to use it.
I've placed my text witch needs to be replaced inside of a div with an id of text. Any ideas?
Define a variable n before that code:
var n = 0;
Remove the n argument from the function, so you have
function(){ return ++n });
The n then refers to the variable you defined and the function requires no arguments.
The argument n was the matched text in the question you referenced. In this case, that text is irrelevant, as it is always z.
document.write is generally not a good idea. It would be better to replace it by assigning to the innerHTML of an element of your choice.
The value passed into the function in the variable n is what's been found in the string. That's a number in the question you've referenced but here it's text (the letter 'z'). Instead of using the text that was found you can just create a global variable and use that instead:
var text = document.getElementById("text").innerHTML;
var num = 0;
text = text.replace(/z/g, function(n){ return ++num });
document.write(text);
(Also note that you need to get rid of the quotes around the 'z' character).
If I read the code correctly. It takes the inner html from a single element with id=text.
e.g the span elements below:
<div id="text">
<span id="exzzzample">maybe CSV data is here z</span>
<span>More CSV Data z z z z</span>
</div>
It then replaces each z with a number that keeps incrementing. This would be:
<span id="ex123ample">maybe CSV data is here 4</span>
<span>More CSV Data 5 6 7 8</span>
The starting value of n can be set outside of this (the line before your code starts) with:
var n = 567; // An arbitrary starting value.
Note that any "z" in the attributes of contained elements will also be replaced as per my example. "z" should at least be very unique if you decide to solve your problem this way.
you need to fix the id of the element you're replacing the text into. This works:
<script>
var n = 0;
function replaceText() {
var text = document.getElementById("text").innerHTML;
text = text.replace(/"z"/g, function(){ return ++n });
// Changed to be placed here as suggested
document.getElementById("text").innerHTML = text;
}
</script>
<div id="text">
"6","z","AAA 101 - College 101:Student Experience","Introduces students to college culture and prepares...","1"
</div>
<input type="button" value="Find" onClick="replaceText();">