I can not call the variable - javascript

document.getElementById("quilltext").value = '[{"insert":"12312312312312312312312312\n"}]';
var x = document.getElementById("quilltext").value;
quill.setContents(x);
doesn't work, but
quill.setContents([{"insert":"12312312312312312312312312\n"}]);
works fine.

From the question, if setContents() with array input works then you can try converting the string value with JSON.parse():
quill.setContents(JSON.parse(x));

Related

Output decodeURIComponent string won't work

In my AngularJS script, I transform part of an URI using VanillaJS decodeURIComponent like this:
var someVar = decodeURIComponent(uristring); //uristring = Zao0%2B1
But when outputting someVar using alert() or placing in inside of an input field, I keep getting Zao0%2B1 instead of Zao0+B1 as an output. What is going on?
Looks like your input is incorrect for the expected result.
If we take the expected result and encode it a different value is returned than what you are using as input
var str = "Zao0+B1"// your expected output
// encode it
var encoded = encodeURIComponent(str);
console.log(encoded);// "Zao0%2BB1" - differs from your input
// decode it
var result = decodeURIComponent(encoded);
console.log(result); // "Zao0+B1" - same as original and as per expected result in question

Storing array in localStorage with JSON in javascript

I have refereed this question and worked for me: so q1
Now Issue is that I have used JSON.stringify & JSON.parse to store array in localStorage. But when I run the code again and try to use JSON.parse on localStorage nothing get retrived. I dont know whats going wrong.
here is code:
function BankProDisplay() {
var myString = JSON.parse(localStorage['bankpro']);
var mySplitResult = myString.split(",");
for (i = 0; i < mySplitResult.length; i++) {
document.getElementById('bankpro').innerHTML = document.getElementById('bankpro').innerHTML + mySplitResult[i] + "<br/>";
}
}
var myString = JSON.parse(localStorage['bankpro']);
JSON.parse returns a javascript object, not a string, which would not have a split method.
If you store an Array in LocalStorage using JSON.stringify, then you get it back using JSON.parse, return value is Array, not string. So when you are using function mySplitResult for myString (which is actually Array, thanks to JSON.parse), then it leads to an Error. So remove function mySplitResult and it should work fine.

Javascript Computed Values With Arrays

Jquery Each Json Values Issue
This question is similar to above, but not the same before it gets marked duplicated.
After realasing how to use computed values i came across another issue.
In my javascript i have the following code:
var incidentWizard = ['page1.html','page2.html','page3.html'];
var magicWizard = ['page1.html','page2.html','page3.html'];
var loadedURL = 'page1.html';
The input to this function would be (true,'incident')
function(next,wizardname)
{
var WizSize = incidentWizard.length;
wizardName = [wizardName] + 'Wizard';
var wizardPOS = jQuery.inArray(loadedURL,incidentWizard);
And now i want to use the wizardname parameter to decide what array i am going to use...
Loader(incidentWizard[wizardPOS],true);
Ive also tried
Loader([incidentWizard][wizardPOS],true);
and
Loader([incidentWizard][wizardPOS],true);
Also the loader function just required the string value in the array at wizardPOS sorry for confusion
But when trying this i always end up with the outcome...
/incidentWizard
I know this is something to do with using computed values but i've tried reading about them and cant seem to solve this issue.
Basicly i want to use the computed value of wizardName to access an an array of that name.
Please help supports, looking forward to seeing many ways to do this!
On this line:
wizardName = [wizardName] + 'Wizard';
You are attempting to concatenate the string 'Wizard' to an Array with one string element "incident". I'm assuming you just want regular string concatenation:
wizardName = wizardName + 'Wizard';
However, now you only have a string, not an array instance. To fix that, change the way you define your *Wizard arrays to something like:
var wizardyThings = {
incidentWizard : ['page1.html','page2.html','page3.html'],
magicWizard: ['page1.html','page2.html','page3.html']
};
Then your function (which is missing a name as it stands), becomes:
function someMethod(next, wizardname) {
wizardName = wizardName + 'Wizard';
var wizSize = wizardyThings[wizardName].length;
var wizardPOS = jQuery.inArray(loadedURL, wizardyThings[wizardName]);
...
}
You can only access properties of objects that way. For global values, window[ name ] will work. For simple local variables it's just not possible at all. That is, if inside a function you've got
var something;
then there's no way to get at that variable if all you have is the string "something".
I would just put each array as a prop on an object:
var obj {
incidentWizard: ['page1.html','page2.html','page3.html'],
magicWizard: ['page1.html','page2.html','page3.html']
};
Then you can just do obj['incidentWizard'] or obj.incidentWizard this will return:
['page1.html','page2.html','page3.html']

Partial string replace in jquery

Just wandering will it be possible to partially string replace in jquery?
I have try to using the following code, but this is not seem working for me:
var test = "testing_supplyAddress_001";
test.replace('supplyAddress', 'billingAddress');
I tried to replace only supplyAddress to billingAddress so the output will be testing_billingAddress _001
JavaScript strings are static and thus .replace() does not actually modify the string. You'll need to assign the value returned by the .replace() function back to the variable:
var test = "testing_supplyAddress_001";
test = test.replace('supplyAddress', 'billingAddress');
Here's a demo showing this in action ->
It works fine. It doesn't replace it in place though - the replace() method returns a new string.
var test = "testing_supplyAddress_001";
var newTest = test.replace('supplyAddress', 'billingAddress');
alert(newTest);
This is just plain old javascript - but will work with jQuery too.
var test = "testing_supplyAddress_001".replace('supplyAddress', 'billingAddress');

3 Simple Javascript lines NOT WORKING !

these lines :
var theSrc=document.getElementById('sb_image_'+id).src;
var srcLn=(theSrc.length)-12;
theSrc.substr(0,srcLn);
are Not working ! , the output is the same as document.getElementById('sb_image_'+id).src if i alert(theSrc);
You need to re-assign the variable to the result:
theSrc = theSrc.substr(0, srcLen);
The call to substr does not change the original string. Instead it returns a new string to you.

Categories

Resources