views:

139

answers:

3

Hi folks,

I just modified an older code and now nothing seems to be working. Could you please guide where am I going wrong.

Some of the things that aren't working are:

Earlier, focus would always stay on the only input field present on the screen. (Now it doesnt), also the if else conditions in the code arent working. On keyup function isnt working either.

<script language="javascript">
    $(document).ready(function() {
        $("#serialNumber").focus();
        $("#scroll-pane").jScrollPane();
        $(document).keydown(function() {
            $("#serialNumber").focus();
        });
        var code = $("#hiddenSerialCode").val();

        $("#serialNumber").keyup(function() {
            var $this = $(this);
            var d = $(this).val();
            var d5 = d.substring(0, 5);

            if (d.length != 20) {
                alert('Incorrect Serial Code');
            }
            else if (d.length == 20 && d5 != code) {
                var serialCode = d.substring(0, 8);
                alert('Serial Code: ' + serialCode);
                $(this).val("");
            }
            else if (d.length == 20 && d5 == code) {
                var flagNumber = d.substring(0, 6);
                var errorNumber = d.substring(7, 10);
                alert('Flag Number ' + flagNumber + '\nError Number ' + errorNumber);
                $(this).val("");
            }
        });
    });
</script>

HTML Code: --------------

<table>
  <tr>
    <td>
    <input type="text" id="serialNumber" class="" />
    <input type="hidden" id="hiddenSerialCode" value="01327"/>
    </td>
  </tr>
</table>

<div id="scroll-pane" class="scroll-pane">

Old working Code: ------

$(document).ready(function() {
    $("#serialCode").focus();
    $(document).keydown(function() {
        $("#serialCode").focus();
    });

    $("#serialCode").keyup(function(){
        var $this = $(this);
        var d = $(this).val();
        if (d.length >= 5){
            //$this.attr("disabled","disabled")
            var $code = d.substring(0,8) ;
            alert('code is ' + $code);

        }
    });
});
+1  A: 

The first thing you need to do is get a javascript trace/debugger for your browser. The one built-in to Chrome is ok, but not great, and FireBug for Firefox is excellent.

Paul Tomblin
This is the HTML. Nothing fancy in here
t3ch
Firebug is for more than HTML. It will also allow you to set breakpoints, watch variables, etc. It's definitely worth it to get Firebug and spend some time figuring out how it works.
Cyrena
@ Cyrena, I just downloaded firebug, trying to figure out how to set breakpoints. Do you see anything wrong in my code ?
t3ch
@t3ch - See my answer. Your main problem is that `.jScrollPane()` is undefined. - You have to remember to add in that plugin.
Peter Ajtai
A: 

@t3ch - Could you also post your html..also, what browser(s) are you testing in?

Chris
Hi Chris, just edited the question with HTML
t3ch
and I am testing in FF 3.6
t3ch
Chris, this should be a comment, since it is not an answer.
Peter Ajtai
Noted Peter..still green
Chris
+1  A: 

The main problem with your code is that $("#scroll-pane").jScrollPane(); refers to an undefined method.

If you take that line out your code functions.

So, you have to make sure to include the jScrollPane() plugin somewhere in your code.

If you just want to scroll to #scroll-pane use .animate():

$('html,body').animate({
    scrollTop: $('#scroll-pane').offset().top},
    'slow');

Additionally, your if statement should probably be if (d.length > 20) otherwise you get an error on every single keypress. You could do a check with .blur() for if (d.length != 20).

Also, you might want to check for a valid serial number before d.length of 20, since it looks like a serial number only has a length of 9.

Finally, as a note, you define $this as $(this), but then you use $(this). For values you could simply use this.value.

$(document).ready(function() {
    $("#serialNumber").focus();
    // $("#scroll-pane").jScrollPane(); - Include plugin if you want to use this.
    $(document).keydown(function() {
        $("#serialNumber").focus();
    });
    var code = $("#hiddenSerialCode").val();

    $("#serialNumber").keyup(function() {
        var $this = $(this);
        var d = $this.val();
        var d5 = d.substring(0, 5);

        if (d.length > 20) {
            alert('Incorrect Serial Code');
        }
                            // Should this really be 20? and not 9?
        else if (d.length == 20 && d5 != code) {
            var serialCode = d.substring(0, 8);
            alert('Serial Code: ' + serialCode);
            $this.val("");
        }                   // What about here?
        else if (d.length == 20 && d5 == code) {
            var flagNumber = d.substring(0, 6);
            var errorNumber = d.substring(7, 10);
            alert('Flag Number ' + flagNumber + '\nError Number ' + 
                   errorNumber);
            $this.val("");
        }
    });        
});

jsFiddle example

Peter Ajtai
Hi Peter, thanks for the help. I didnt get this part "your if statement should probably be if (d.length > 20) otherwise you get an error on every single keypress". Also I cant use onBlur event, reason being, as soon as either of the IF conditions are met, I am going to make an Ajax call. for onblur, I guess I need the input field to be out of focus?
t3ch
The blur was just a suggestion. ---- An if statement of `if (d.length != 20)` will produce an alert of `Incorrect Serial Code` for every single keyup event until you enter 20 characters..... 20 alerts, no matter what. So maybe you should only check if the serial number entered has become too long using `if (d.length > 20)` ------- Try it out with `!=20` ==> http://jsfiddle.net/qFVE6/
Peter Ajtai
@t3ch - And also, you might want to check for a valid serial number before `d.length==20`, since a serial number only has to be 9 long.
Peter Ajtai
Hi Peter, Thank you very much. Since last few days I was confused for the problem that why its throwing alerts multiple times whenever I am doing d != 20.
t3ch
Peter, even after removing jScrollPane code, I am not able to get the focus
t3ch
@t3ch - That's odd. When I open this page - http://jsfiddle.net/kkcvL/ - the focus starts on `#serialNumber`. Whenever you want to bring focus to that element use [ **`.focus()`** ](http://api.jquery.com/focus/) like: `$("#serialNumber").focus();`
Peter Ajtai