Hello I am wondering why the value is not changing in my key, value pair set. I believe its not changing because it looks the same before and after using console.log(). I have a picture of the console. It might be an easy solution that has to do something async happening but I can not think of the solution! The code looks like:
console.log(files[index])
files[index].name = "lol"
console.log(files[index])
I have replicated your issue in the console, and can see it does work.
You need to make sure your 'index' is in quotes.
Related
below is my console output...
console.log(data);
console.log(JSON.stringify(data));
console.log(data.errorMessage);
Console shows....
{"errorMessage":"Registration Successfull"}
"{\"errorMessage\":\"Registration Successfull\"}"
undefined
How come it is undefined?I also tried data['errorMessage'] still the same output undefined how do we fix this?
Looks like you omitted some code. And your data is changed from Object to String. So try JSON.parse(data).errorMessage in your last string.
Something turned data into a string so it no longer has properties on it. This should never be seen: "{\"errorMessage\":\"CA Registration Successfull\"}". Something in your code is changing the data type on you.
I would not use JSON.parse as Vasyl suggested right away. You need to find out why it turned to string and not just turn the string back to object.
not sure where the errors lies in what I am trying to achieve.
I am working with someone else's code and unfortunately they have used numbers for div ids in some places.
These number ids are used in various places and if I can, I want to find a way to keep things as they are.
So,
returning the following in JSON:
editorID: "1000"
And in my AJAX call i use that return like so:
var editorID = response.editorID;
CKEDITOR.instances.editorID.insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
However this gives me an error saying that the editorID is undefined.
As you can I already use a JSON response in my code, this works fine so its not a problem with datatypes etc.
I also tried to do:
alert(response.editorID);
which gave me the correct value.
When I tried putting a number directly into CKEditor insertHTML code it was showing my syntax errors so maybe thats the issue. If so, any work around for it?
Thanks. Craig.
To use a variable as a property, you have to use [] notation:
CKEDITOR.instances[editorID].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
When you use .editorID, it's looking for a property named editorID, not 1000.
You also have to use this syntax when the property isn't a valid identifer. So if you wanted to put the number directly, you would write:
CKEDITOR.instances['1000'].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
Firstoff I'd like to add I've been learning javascript for like only 2 days now. I'm pretty much way ahead of myself with what I'm trying to get but here goes.
I have a json array from which I get data to replace/insert in my page. The first problem I have is that if it comes across an empty ('null') key it will just stop. Will not even try to continu.
document.getElementById("id1")src=json.img1.link;
document.getElementById("id2")src=json.img2.link;
document.getElementById("id3")src=json.img3.link;
json.img2.link is empty ('null' response from json.). javascript will then not replace "id2" but it also won't replace "id3".
I'm now trying to find a solution where it will if nothing else at least set a default.
The script is not continuing executing because it comes to an error --trying to access property link of an undefined object
Try
document.getElementById('id2').src = json.img2 ? json.img2.link : 'defaultLink';
This way your are checking for undefined (ie null) object in img2 and assigning the default value. This assumes that what is not defined (null) is the img2 object.
Actually I don't think your code should work at all, you are missing the. before the src So, try
document.getElementById("id1").src=json.img1.link;
document.getElementById("id2").src=json.img2.link;
document.getElementById("id3").src=json.img3.link;
and let us know if that doesn't solve the problem.
Btw, +points for learning JavaScript and not just straight into jQuery!
This has been driving me crazy.. I am trying to return a value from a variable and I can't seem to do it. When I multiply the variable by something, it works fine, but when I try to just show that one variable on a keyup function, it's a no go.
This is the problem in the script I am running into:
$("input.numberOfAccounts").keyup(function () {
$("input.pricingPerAccount").val($(newPricePerAccount));
});
As you can see, newPricePerAccount is the value I want to return and I have tried everything to make it just return that. What am I doing wrong?
Here it is in a jsfiddle, it will give you more insight.
http://jsfiddle.net/GLnQx/
EDIT: I have updated the fiddle.. All I am really trying to do is match input.pricingPerAccount to the variable of newPricePerAccount and it keeps returning just object, object. What's wrong?
Is this what you wanted? http://jsfiddle.net/GLnQx/2/
EDIT:
If I understand your comment correctly, you want to re-update the value of newPricePerAccount each time as well? Is this what you mean? http://jsfiddle.net/GLnQx/6/
var newPricePerAccount = $("input.pricingPerAccount").val() * $("input.numberOfAccounts").val();
alert(newPricePerAccount);
Is that what you are looking for?
The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Yes something changed in Firefox 5+
However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3