2

This script returns a value like 33 | 4 and I need only the the first whole number, in this case 33.

Can I use .replace( /[^\d].*/, '' ) and where/how to put this? Or is there better solutions?

You are all very helpful, but i'm in a stage where I need to see my full script or a line from it with you solutions implemented ;-)

Thanks

jQuery(document).ready(function($) {
  $("#input_1_1").blur(function() {
    // sets the value of #input_1_8 to
    // that which was entered in #input_1_1
    $("#input_1_8").val($("#input_1_1").val());
  });
});
Pete
  • 45
  • 4

3 Answers3

1

You can use parseInt() or split your string with | and get the first result

console.log(parseInt("33 | 4"))
// or
console.log("33 | 4".split(" | ")[0])

In your case you can do

jQuery(document).ready(function($) {
  $("#input_1_1").blur(function() {
    $("#input_1_8").val(parseInt($("#input_1_1").val()));
  });
});
R3tep
  • 12,512
  • 10
  • 48
  • 75
0

Try this,

var str = "33 | 4";
var res = str.split("|");//split with '|' it will return array
console.log(res[0].trim());//33

You can do trim on it or parseInt, whatever you want to get your task done.

M.Hemant
  • 2,345
  • 1
  • 9
  • 14
-1

With split you can split a string with any character, my example:

<script>

var stringexample="45.89 | 65";
alert(takefirst(sting));
}

function takefirst(){
var a=string.split("|");
return a[0]
}
</script>

Also you can use split for separete character for character like this

var a=stringexample.split("");
for(var i=0;i<a.lenght;i++){
alert(a[i]);
}

Alerts:

4
5
.
8

Etc..