1

I have a textbox whose value comes from database but if a user changes the value then this value needs to be used in a calculation.

$('#ecost input.ecost').keyup(function(){
    if (!isNaN(this.value) && this.value.length != 0) {
        var Cost = $(this).val();
    }
});

and

var cost = $('input.ecost1').val();

I need if keyup function for user enter value (first code example) else default database value (second code example). How can I write this if/else condition?

i need

 if ($('#ecost input.ecost').keyup(function(){
    if (!isNaN(this.value) && this.value.length != 0) {
        var Cost = $(this).val();
    }
}); ) 
else {
var cost = $('input.ecost1').val();
}

i know in if() code is wrong but above logic how to corrected

2
  • 1
    And what is wrong right now? Commented Apr 15, 2015 at 7:49
  • how to place if else condition to get one of value above i want if keyup than that value other wise dfault (below value) Commented Apr 15, 2015 at 8:14

5 Answers 5

2

If I understand correctly, this is what is required.

var valueFromDB = getValueFromDB(); //Your function for calling database
var Cost = 0;
$('#ecost input.ecost').keyup(function(){
    if (!isNaN(this.value) && this.value.length != 0) {
        Cost = $(this).val();
    }
    else{
        Cost = valueFromDB;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

if-else in javascript works like this:

if(expression) {
    //do this if expression is true
} else {
    //do this if expression is false
}

1 Comment

thanks but i need for keyup what is condition i have to write.
0

Is it that you want?

var Cost = $('input.ecost1').val();
$('#ecost input.ecost').keyup(function(){
    if (!isNaN(this.value) && this.value.length != 0) {
        Cost = $(this).val();
    }
 }

2 Comments

according to your answer $('#ecost input.ecost').keyup(function(){ this keyup function must but i need if keyup else default value
then use the default value outside the handler, I edited the answer
0

You do not need to include the else if you provide a default value before you evaluate your input:

UPDATE as per comments below.

var Cost = 123; // default value;

$('#ecost input.ecost').keyup(function(){

    if (!isNaN(this.value) && this.value.length != 0) {
        Cost = this.value;
    }
});

2 Comments

according to your answer $('#ecost input.ecost').keyup(function(){ this keyup function must but i need if keyup else default value
@NachiketChaudhary I have updated my answer. By initialising a default value Cost on page load for instance, any time the keyup function is then executed, a new value will replace the default Cost.
0

Thank you for the answers, I have this problem also and I edit @onerror answer. It works well

var search = $("#teacher_year_filter").val();

$("#teacher_year_filter").on("keyup", function() {
    if (!isNaN(this.value) && this.value.length != 0) {
        search = this.value
    }

    var table_1 = table1.column(3).search(search).draw();
});

var table_2 = table1.column(3).search(search).draw();

It will filter the default value in the input, and once we type (keyup), then it will filter using the current input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.