1

I am trying to pass value of xyz from function to a new var abc, but its not working. I tried like abc="'"+xyz+"'"; (Not working) If I try like abc='anything'; it works and i can use abc value to new function in document.ready function. I want dynamically generated value of xyz, for abc with single quotes. With all your suggestion I am updating the code, i tried all the way... but dont know why abc is not catching the value of xyz or newcityval(); Also I have one line script above the code, where default value of abc is specified. Now when city value changes city_val gets updated and same i check using alert function. Also for abc if i put any value manually like abc='anything'; and alert it work fine. But using all the suggesstions its not working. I also declared global vars for both xyz and abc. updated code

<script type='text/javascript'>abc = 'any value';</script>
<script type='text/javascript'>
var xyz;
var abc;
function newcityval(){  
    xyz = $("#city").val();
    document.getElementById('city_val').value=xyz;
    return alert("'"+xyz+"'");

}

abc=newcityval();
if(abc!='')
{
alert("value is "+abc);
}
else
{
    alert('No value');
}

</script>

Thanks

2
  • 4
    var abc = newcityval()? Commented Aug 4, 2014 at 11:48
  • 1
    abc="'"+xyz+"'"; isn't working because xyz hasn't been declared in that scope. Try: abc=newcityval(); I'm guessing that is more what you want. Commented Aug 4, 2014 at 11:50

3 Answers 3

1

If you want to use the returned value of your function use:

abc = newcityval();

In abc="'"+xyz+"'"; the xyz is an undefined variable. because you define your xyz variable inside your function.


Update:

set script like this:

<script type='text/javascript'>
    function newcityval(){
        xyz = $("#city").val();
        document.getElementById('city_val').value=xyz;
        return "'"+xyz+"'";
    }

    function run()
    {
        abc=newcityval();
        if(abc!='')
        {
            alert("value is "+abc);
        }
        else
        {
            alert('No value');
        }
    }
</script>

HTML code:

<input id="city" />
<input id="city_val" />
<button onclick="run()">do</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your code helped me to update my code with some changes and things are working fine.
1

Try abc = "'" + newcityval() + "'"

You are declaring xyz local to the newcityval function so it is unavailable outside.

Comments

0

you need to type

var abc = newcityval();

xyz is a variable inside the scope of newcityval, which means it's not available when you are trying to assign it to abc.

Comments

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.