Home > DeveloperSection > Forums > how to call a function on textchange in textfield using java script
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    April-04-2013 2:03 AM

 JavaScript JavaScript 
Ratings:


 2 Reply(s)
 1075  View(s)
Rate this:
Hi Mindstickians!

I have textfiedl on which i am calling a function which is given below but problem is that it is working on enter but not working on keyup or as the value 

of textfield changes i want to call this method.

Javascript:

$(function() {

    var input = $('.input'),
        bar = $('.bar'),
        bw = bar.width(),
        percent = bar.find('.percent'),
        ps = percent.find('span');

    input.on('keydown', function(e) {
        if (e.keyCode == 13) {
            var t = $(this),
                val = t.val();
            if (val >= 0 && val <= 100) {
                var w = 100 - val,
                    pw = (bw * w) / 100,
                    pa = {
                        height: w + '%'
                    },
                    cw = (bw - pw) / 2,
                    ca = {
                        left: cw
                    }
                ps.animate(pa);
                cs.text(val + '%');
                circle.animate(ca, function () {
                    circle.removeClass(name)
                }).addClass(name);
            } else {
                alert('range: 0 - 100');
                t.val('');
            }
        }
    });

});
Html:

<div class="text">
    <input type="text" class="input" value="0" id="sliderValue170h" />
</div>

Thank in advance!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-04-2013 7:13 AM

Hi Jacob!

just add change to the function that it works for both keydown and change:

$(function() {

var input = $('.input'),
    bar = $('.bar'),
    bw = bar.width(),
    percent = bar.find('.percent'),
    ps = percent.find('span');

input.on('keydown change', function(e) { // in here it works for both keydown and change
    if (e.keyCode == 13) {
        var t = $(this),
            val = t.val();
        if (val >= 0 && val <= 100) {
            var w = 100 - val,
                pw = (bw * w) / 100,
                pa = {
                    height: w + '%'
                },
                cw = (bw - pw) / 2,
                ca = {
                    left: cw
                }
            ps.animate(pa);
            cs.text(val + '%');
            circle.animate(ca, function () {
                circle.removeClass(name)
            }).addClass(name);
        } else {
            alert('range: 0 - 100');
            t.val('');
        }
    }
});

Vijay Shukla

Total Post:100

Points:700
Posted on    April-04-2013 7:25 AM

Hi Jacob!

you can use onkeypress event like this :

HTML

   <div class="text">
   <input type="text" class="input"  value="0" onkeypress="return runScript(event)"   id="sliderValue170h"/>
   </div>
Javascript

 function runScript(e)
 {
     //Set Your logic
      if (e.keyCode == 13) {

      }
 }

Don't want to miss updates? Please click the below button!

Follow MindStick