0

Im newbie in JS so please bear with me.

I have a JS variable that i got from web service (JSON type). I tried to alert this variable and its working, returning 1 (integer) :

alert(result[0].totalMall);

I want to add a string to this variable so it will become like (for example) : 1 mall(s)

This is my code :

var result = result[0].totalMall.toString() + "Mall(s)";
                        alert(result);
                        $('.totalMall', this).html(result);

But the alert above always returning undefined. What should i do? I have been working for hours to just fix this.

And the result always displaying just "mall(s)" without the integer (result[0].totalMall.toString()) with it.

Any help is appreciated. Thanks :D

3
  • It may be having problems with the fact that you have two things named result. Try var res = result[0].totalMall.toString() + "Mall(s)";, and see if that's any better. (Then alert(res) etc, of course.) Commented Aug 1, 2013 at 14:15
  • 3
    Your code doesn't match your described results, and in fact it works here: jsfiddle.net/cr5ud. Can you post a fiddle that reproduces the issue? I think you may be having an issue with the fact that you are overwriting your result variable (though it doesn't affect the code you've posted). Commented Aug 1, 2013 at 14:15
  • try without .toString() sum of integer and string always returns string :) Commented Aug 1, 2013 at 14:28

2 Answers 2

1

try following code:

var results = [
  {
    totalMall :3
  },
  {
    totalMall :5
  }
];

var result = results[0].totalMall.toString() + " Mall(s)";
alert(result);
$("#div").html(result);

Working example: http://jsfiddle.net/WaydY/

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

Comments

0

Thanks for your help, all :D

yes, it appears that "something" was wrong last night. I tried the code this morning, and its working fine now :D

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.