0

i have a table like below which each content is input pop-up enter image description here

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 enter image description here

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

Andriansyah
  • 209
  • 1
  • 2
  • 12
  • You want to know what the last digit is and test if it is equal to one of those 4 values? Simple reg exp can do that. – epascarello Jul 12 '13 at 02:48
  • I'm sorry, but your sentences aren't very clear... Bind the last digit? I don't really understand what it means. – Ariane Jul 12 '13 at 02:49
  • Do you know how to do it in regular Javascript? You can use standard JS inside of a jQ function. – DevlshOne Jul 12 '13 at 02:50
  • yes, you see in my table there value A1.1.1,A1.1.2,A1.1.8 and A1.1.9 for an example. i need to bind the last digit..Simple reg exp like what? – Andriansyah Jul 12 '13 at 02:52
  • @Ariane sory about my english...i mean to get the last digit of "Posisi" value. if the last digit value like 1,2,8,9 so the line value will "9" or the other that i need – Andriansyah Jul 12 '13 at 02:58
  • @AndriansyahAndri, Hey, can you please post all your code in fiddle, so that i will try to help you out, and also please be clear with your requirement. – RONE Jul 12 '13 at 03:48
  • 1
    @AndriansyahAndri Uhm... `string.substring(string.length-1)`... Something like that, maybe? (syntax not guaranteed; tired.) – Ariane Jul 12 '13 at 06:15

2 Answers2

0
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);
}
Malk
  • 11,855
  • 4
  • 33
  • 32
  • ya but the problem in my table also have another value like A1.9.9. the script will error..not fullfill my purpose – Andriansyah Jul 12 '13 at 03:01
  • for $("#line").val("i can add whatever i want") as long as fullfill conditonal that $("#posisi") last digit value between 1 or 2 or 8 or 9. – Andriansyah Jul 12 '13 at 03:33
0

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.

Community
  • 1
  • 1
Haris Amjed
  • 171
  • 1
  • 7
  • hm...i agree this answer if i have value just 1. but how to general this function...because i have another...value to get – Andriansyah Jul 12 '13 at 03:11
  • Well as I understand you want to pass multiple values to 1 input field or other dom element. You can do so using html5 data- attibute. have a look at [html5 data attribute](http://html5doctor.com/html5-custom-data-attributes). Hope this helps – Haris Amjed Jul 12 '13 at 03:18