1

I am using a script with jQuery:

$(document).ready(function () {
    var button;
    var line;
    var inputs;
    var params = {};
    var updatefield;
    $('button.update').click(function () {
        button = $(this);
        params['button'] = button.val();
        line = button.closest('.line');
        updatefield = line.find('td.resultFromGet');
        inputs = line.find('input');
        inputs.each(function (id, item) {
            switch($(item).attr('type')){
                case 'checkbox':{
                    params[$(item).attr('name')] = new Array($(item).is(':checked'));
                    break;
                }
                default:{
                    params[$(item).attr('name')] = new Array($(item).attr('value'));
                    break;
                }
            }
        });
        //alert(JSON.stringify(params, null, 4));
        $.get( 'core/ajax/correct_exec.php', params )
            .done(function (data){
                if(data == '1'){
                    $(updatefield).html('UPDATE_RECORD_SUCCESS');
                } else {
                    $(updatefield).html( data );
                }
            });
    });
});

The page I am getting is doing echo '1'; from PHP in case of success. I try to test this with data == 1 but it doesn't work even though it is a success. In fact, it sends me $(updatefield).html( data ); which is 1. So why can't it just print UPDATE_RECORD_SUCCESS?

7
  • 2
    updatefield is already a jQuery object, so you can just do: updatefield.html('UPDATE_RECORD_SUCCESS'); Commented Feb 28, 2014 at 16:12
  • 1
    try console.log(data); Commented Feb 28, 2014 at 16:12
  • 1
    also try console.log(typeof data) Commented Feb 28, 2014 at 16:13
  • console.log($(updatefield).length); so that you know if it exists. But still id recommend to use chrome and put a breaking point on that line of code: if(data == '1') Commented Feb 28, 2014 at 16:16
  • how can I do that? I never used any debug tool for javascript, only try and errors. Do you have a link where I can see how to do breaking point and use chrome for that. In fact, I didn't even know it was possible so I guess learning that will help me so much in finding my errors in the future :). Commented Feb 28, 2014 at 16:17

1 Answer 1

2

The data response is coming with white space, if you use trim() function something like this, then if condition should be executed.

if(data.trim() == '1'){
     $('#updatefield').html('UPDATE_RECORD_SUCCESS'); //this should be executed
} else {
  $('#updatefield').html( data );
}

Here you can see the space with data in chrome debugger.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, how did you manage to see this in chrome debugger? I used a console.log(data) but I don't see the little arrow showing there is a return at the EOL.
For this you will have to use breakpoint to line if(data == '1'){, for information about breakpoint see developers.google.com/chrome-developer-tools/docs/….

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.