user3341223
user3341223

Reputation: 13

JQuery: Change value when value of other input field is changed with a range of numbers

I was wondering if someone can help me develop this in Jquery (mobile):

I want to make two text fields, in the first one you have to enter a number and in the other one I want to see a result. I mean something like this:

example 1:

textfield 1: 220 till 285 < input

textfield 2: 1 < result

example 2:

textfield 1: 286 till 350 < input


textfield 2: 2 < result

example 3:

textfield 1: 351 till 415 < input

textfield 2: 3 < result

This must happen automatically and not with pressing a button, etc., like this method

UpdATE! What i mean is more a range like this: input: for example the numberrange 220 till 285 will result in 1. 286 till 350 in 2. 351 till 415 in 3. So all the numbers between for example 351 - 415 must has 3 as output

Upvotes: 0

Views: 2536

Answers (2)

lschmierer
lschmierer

Reputation: 616

Simply use the keyUp event provided by jquery:

html

<input type="text" id="input" />
<input type="text" id="output" />

js

$('#input').keyup(function() {
    $('#output').val($('#input').val().substring(0,1));
});

http://jsfiddle.net/uGLpt/

EDIT

You can check the input with simple if-conditions:

$('#input').keyup(function () {
    var value = $('#input').val();

    if(value >= 220 && value <= 285)
        $('#output').val(1);
    else if(value >= 286 && value <= 350)
        $('#output').val(2);
    else if(value >= 351 && value <= 415)
        $('#output').val(3);
    else
        $('#output').val("");
});

http://jsfiddle.net/uGLpt/4/

Upvotes: 1

A J
A J

Reputation: 2140

Is this what you want fiddle

$(document).ready(function(){
$("#txt1").keyup(function(){
    $("#txt2").val($("#txt1").val()/50);
});
});

<input type="text" id="txt1" />
<input type="text" id="txt2" />

Upvotes: 1

Related Questions