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);
}
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 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 :-)
I'm having a trouble when i try to force the user to enter data to a text input. I need to do something like the IP Address input in Windows.
I want to split the text input by dashes having something like this
10 - 10 - 10 - 10
Is there any way to do this ?
http://jsfiddle.net/87dug9oa/1/
function check(text) {
var result = [];
text = text.replace(/[^\d]/g,"");
while (text.length >= 3) {
result.push(text.substring(0, 3));
text = text.substring(3);
}
if(text.length > 0) result.push(text);
$("#ip").val(result.join("-"));
}
$("#ip").on("keyup", function() {
check($(this).val());
});
This creates a function, which adds dashes once 3 characters has been added (and the fourth is written).
Now, this does do what you want, but you need to add some additional stuff, such as checking for length and making the remove part work (because when you press any key, it will change the input's value, which will make the caret move to the last character).
Oh I almost forgot. This can be changed to the length of your choice, of course. Just change the "3" to be something else.
Okay, I'm doing an exercise to learn Javascript, where I need to make a simple Sudoku app. There's a function to create the Sudoku field, and each little square is in a div with an id identifying the row number and column number.
Now the idea is that if a user clicks on an open field, a prompt appears asking him/her to enter a number. If the number is between 1 and 9, that number is then displayed inside the field.
I first invoked the function like this:
node.onclick=function(){fillNumber(this.id);};
This had the unexpected side-effect of making the prompt box appear three times in a row whenever a click was performed. Yet, it was clear that the first input by the user was accepted, stored and added to the div just like it was supposed to. The input from the second and third prompt box is simply lost.
I solved the problem by using the following invocation:
node.onclick=function(){if (parseInt(this.id) > 0) fillNumber(this.id);};
Yet I've no idea why this works (this is copied from a fellow student who did it this way, but doesn't know why). The value of this.id is always something like this: "11", "12", "13", "21", "22", ... So I don't even see the point of first parsing it to an int or checking whether it's bigger than 0. It always is both an int and bigger than 0 as far as I can see. Regardless, the code of the method itself didn't change and the method is invoked with the exact same argument value.
Here's the method fillNumber:
function fillNumber(id){
var input = -1;
do{
input = parseInt(prompt("Enter a number between 1 and 9: ", ""));
}while(input < 1 || input > 10);
var i = parseInt(id/10), j = id%10;
numbers[i][j] = input;
var tekst = document.createTextNode(numbers[i][j]);
document.getElementById(tekst).appendChild(tekst);
}
Can anyone explain this to me?
I may suggest you to check whether there are elements upon each other - this might be the reason for three prompts in a row.
I guess the reason the other student wrote parseInt(this.id) is the same.
He tries to parse an id into an int to escape the other clicked elements, so he makes sure the id is a valid number.
Try to use next lines:
node.onclick=function(e){
if( e.stopPropagation ) e.stopPropagation();
if( e.preventDefault ) e.preventDefault();
else e.returValue = false;
fillNumber(+this.id);
}
Note the + at +this.id parses a string to an integer or a float.
I have a text field that can accept input of any kind. Based on the input, I need to make some fields hidden and others unhidden. However I want to put another condition within the first condition to check for the first 3 characters of the input value
Here is my code for the first condition:
$("#accountcodes").live("focusout",function(){
var code = $(this).val();
if(code>30000){
alert("T1 to T4 codes needed");
$(this).parents("tr").find('#T1').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T2').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T3').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T4').removeAttr('hidden','hidden');
}
})
I want to put another condition within the if statement(if within an if) to check if the first 3 characters start with 310 or 311 then do something else e.g if a user inputs 31102, then another field is unhidden e.t.c I am not sure how to do that in Jquery. should I use regex? do I take the value of input and cut out the first three characters and check it?
Any help will be appreciated
So you just want to know about substr()? code.substr(0,3) would get you the first three characters.
Alternatively, you may want to use a regex to find 310 and 311 more easily, in which case you want code.match(/^31[01]/) instead.
var code = $(this).val();
var firstThreeChars = code.substr(0, 3);
It seems your values will always be numeric. You don't need much jquery to achieve this.
$("#accountcodes").live("focusout",function(){
var code = $(this).val();
if(code > 30000){
//Note: For any non-numeric value, this condition will always be false
alert("T1 to T4 codes needed");
var p = $(this).parents("tr");
p.find('#T1, #T2, #T3, #T4').removeAttr('hidden');
if ((code - 31000) > 102){ // Your other condition check can look like this
// unhide other fields
}
}
});
Hope this will help. Happy Coding.
Your Question have your answer you can do it many ways
Sub-string the value and check the value.
2.go for contains key word of jquery [ api.jquery.com/contains-selector/][1]