converting JS variable to string & display it in html - javascript

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

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/

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

Related

Why is javascript variable overlapping?

My question is why I'm having issues with declaring these variables. It works fine until "priceAdj" and the only thing I can find to explain that, Is that Javascript thinks it's still part of the previous variable. But I have no idea why it would think that because isn't the comma supposed to seperate them? I know I could just declare var again, But my code won't let me pass it up.
CODE:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
Logger.log(priceDigits);
}
I'm getting a syntax error of "Unexpected token )" when I run it in the console. You have an extraneous "/" in this line: (Math.ceil(priceAdj*100)/);
So I think I sort of know whats wrong.. the code actually does work, but I thought it didn't because what I'm using didn't make it the same color as the other variables for some reason. I don't know why that is, but I guess that isn't a question for here.A screenshot of the code is below:
Screenshot
Your var declaration is correct.
You can test it in codepen with the following sample which returns 288000 for priceDigits:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
//Logger.log(priceDigits);
return priceDigits;
}
$('body').text(myFunction());
See corresponding codepen here: https://codepen.io/anon/pen/YEPMLb.
Maybe your problem comes from your last instruction.

Passing PHP array to JAVASCRIPT (issue)

There's some weird issue it seems.
I'm trying to pass a php array to js, which I've accomplished. I can display the array size like so
document.getElementById("test").innerHTML = (jarr.length);
and it works perfectly fine, however, when I try to display a value in said array, it says object Object
and what's weird is if I go try to read a value which isnt stored, for example jarr[234] it says undefined because there's no value stored, so I know some data is being transferred.
Any help?
edit: here's how I passed the array to js
var jarr = <?php echo json_encode($testing); ?>;
edit: I forgot the remove the parse part before copying it, I was experimenting beforehand.
You should be able to just directly use jarr without parsing it, for example:
document.getElementById("test").innerHTML = jarr.length;
document.getElementById("first_element").innerHTML = jarr.length > 0 ? jarr[0] : "empty array";
If you are doing the above and the elements are showing up as object Object it means that inside your array, you are storing objects rather than primitive data types. So maybe try something like the following to generate a nice string representation of those objects (noting that this is needed for displaying them, but not for using them directly in your JavaScript code):
document.getElementById("first_element").innerHTML = jarr.length > 0 ? JSON.stringify(jarr[0]) : "empty array";
What Chris said fixed the issue!
"To show the entire array: document.getElementById("test").innerHTML = JSON.stringify(jarr); To show a single string: document.getElementById("test").innerHTML = jarr[0]; – Chris G 7 mins ago
"
For whatever reason adding the (stringify) fixes it for me.
An example
document.getElementById("test").innerHTML = JSON.stringify(jarr[2].title);

applescript with javascript -> get string from href

I try to read out the string inside of an unordered list .
The string of interest is the following number: href="?notation=1549220"
image
I am very new to HTML/CSS/JavaScript so please excuse this simple question.
I tried something like this, but it's getting nowhere:
document.getElementById('exchangesLayerHs')
.getElementsByTagName('ul')
.getElementsByTagName('li')
.getElementsByTagName('a');
Has anyone a tipp for me?
Thanks a lot!
Carlo
You could get the link with the title handelsplatz geschlossen :
var link = document.querySelector("a [title='Handelsplatz geschlossen']");
Now simply get the thing behind = in the href and convert it to a number (+) :
var result = + link.href.split("=")[1];

javascript adding strings together

var body = "jsonString=123";
var mybody = body.replace("jsonString=","");
console.log("mybody:"+mybody);
console.log("myhead:"+body.replace("jsonString=",""));
This this should give two equal outputs to console.log file but it doesnt!
Output:
mybody:
myhead:123
Can someone explain why variable mybody is blank?
Shouldnt it be 123?
EDIT
I am such a fool, there were some unrelated problems with my execution environment that led to this. It has nothing to do with javascript.
Thanks for the help SO
It is giving me the correct output per this jsFiddle
Results:
mybody:123
myhead:123

javascript .search does not return value after &v=

the test variable is a YouTube link from which I am trying to extract pa14VNsdSYM
var test = "http://www.youtube.com/watch?feature=endscreen&NR=1&v=pa14VNsdSYM";
var urll = (test.search(/[?&;]v=([^&;]+)/) || [])[1];
document.write(''+urll+'');
but I am only getting undefined http://jsfiddle.net/ZGjDb/13/
Here's the problem:
You should use match instead of search.
Demo: http://jsfiddle.net/DerekL/ZGjDb/17/
I have figured it out sorry guys but if your'e wondering it's here http://jsfiddle.net/ZGjDb/16/ , REALLY and TRULY sorry users.
use test.match() instead of test.search()

Categories

Resources