0

I have input field and I want to get input value when input loses focus ( if input is empty ) or just get input value ( if input isn't empty )

My input

<input type="text"  id="title" value="Value test" kl_virtual_keyboard_secure_input="on">

And I try

var input_length=$.trim($("#title").val());

function blurTitleValue() {
    $('#title').blur(function() {
        var str = $.trim($("#title").val());
        var str_ref = str.substring(0,3);
        return str_ref;
    });
}
function TitleValue() {
    var str = $.trim($("#title").val());;
    var str_ref = str.substring(0,3);
    return str_ref;
}
if(input_length.length>0)
{
    TitleValue();
}
else{
    blurTitleValue();
}
alert(str_ref);

But I can't get str_ref value

3
  • 1
    Im like 99% sure that alert(str_ref) will be undefined. You need to declare str_ref global and then assign value to it with functions. In your case you declare it localy inside function scope and after function exited you will not have str_ref variable. Commented Aug 27, 2016 at 10:47
  • @MykolaBorysyuk: Close -- it'll be a ReferenceError because it's trying to read the value of an undeclared identifier. Commented Aug 27, 2016 at 10:51
  • Yeah...forget about this :D... But the answer still correct. Ty for correction. Commented Aug 27, 2016 at 10:54

2 Answers 2

1

If you return str_def you are not setting variable str_def of parent scope to its value, you are just losing your value because you didn't assign it to anything. You need to save returned value to your variable.

Try it on JSFiddle.

<script>
    var input_length = $.trim($("#title").val());
    var str_ref;

    function blurTitleValue() {
        return $('#title').blur(function () {
            var str = $.trim($("#title").val());
            var str_ref1 = str.substring(0, 3);
            str_ref = str_ref1;
        });
    }

    function TitleValue() {
        var str = $.trim($("#title").val());
        ;
        var str_ref1 = str.substring(0, 3);
        return str_ref1;
    }
    if (input_length.length > 0) {
        str_ref = TitleValue();
    } else {
        blurTitleValue();
    }
    alert(str_ref);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Note that blurTitleValue never returns anything. And I'd avoid suggesting shadowing the variable like that.
@T.J.Crowder. I really didn't pay atention. But anyway, I think OP undertood it. I edited answer.
0

What you want is a global str_ref variable. You need to declare it globally, and remove the var before declaring str_ref inside the functions. It will scope the variable to the function and when trying to access str_ref, you will be trying to access the global variable (which in your case, is undefined).

Here is the fixed code:

var str_ref;

var input_length = $.trim($("#title").val());

function blurTitleValue() {
  $('#title').blur(function() {
    var str = $.trim($("#title").val());
    str_ref = str.substring(0, 3);
    return str_ref;
  });
}

function TitleValue() {
  var str = $.trim($("#title").val());;
  str_ref = str.substring(0, 3);
  return str_ref;
}


if (input_length.length > 0) {
  TitleValue();
} else {
  blurTitleValue();
}

alert(str_ref);

And a JSFiddle.

It also unclear what you were trying to do with blurTitleValue(). Either way, you won't be able to access the new str_ref because it will only change on blur.

I would also recommend caching your jQuery elements. Instead of always querying $('#title'), declare a global var $title = $('#title'); and use it afterwards. This is best practice.

2 Comments

Why do you return str_ref? It doesn't affect anything.
That is the code provided by the asker. I only fixed the essentials.

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.