I am trying to create a javascript object,
var systemName = {"system" : varA};
But I want the object to be in the form `{"system" :"varA"}
with varA having the variable value but inserted inside double quotes.
I have tried {"system" : "'+ varA +'"};
but that did not do the trick. Can you point out what I am doing wrong here? I know its a simple things. But sometimes these small things get us stuck at certain points
Try this instead
var systemName = {};
systemName.system = varA;
(or)
systemName["system"] = varA;
You don't want to do this. You shouldn't do this. If it is a string, the JSON parser will handle it for you. Don't worry about adding quotes to it. There is no reason for you to put quotes around the literal value of the variable. You can put quotes around it at the time of output, if you need to to.
var varA = "Hello";
var systemName = {"system" : varA};
console.log(JSON.stringify(systemName));
// {"system":"Hello"}
http://jsfiddle.net/FWBub/
But, if you must do so:
var varA = '"Hello"';
var systemName = {"system" : varA};
console.log(JSON.stringify(systemName));
{"system":"\"Hello\""}
http://jsfiddle.net/FWBub/1
JSON.stringify(varA) will add JSON quotes around the value.
Related
I need to get a id from a html element and replace a part of the word. For example:
HTML
<input type="checkbox" id="facebookCheckbox"></div>
JavaScript
var x = document.getElementById("facebookCheckbox");
var name = x.id;
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced. Is there a different way of doing this?
I'm looking for purely javascript no jQuery
Thank you!
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced.
No, it does work and there's no need to be "standalone" - any part of the string can be matched. Only you did nothing with the result of the operation:
console.log(name.replace("Checkbox",""));
// or
name = name.replace("Checkbox","");
// or assign back to x.id maybe?
You are creating a copy of string when replacing, so you must assign the result of .replace() back to x.id.
var x = document.getElementById("facebookCheckbox");
x.id = x.id.replace("Checkbox","");
this is not going to work in this way. However you can have a marker kind of character by which you can break the name into array and implement the logic. For example:
var x = document.getElementById("facebook_Checkbox");
//Note I have added underscore in the Id
var name = x.id;
var arr=name.split("_");
//Now you have Checkbox and Facebook as string objects (part of array) and you can use them
name=arr[0]
I hope it will solve the purpose.
SOLVED: read edit below
First of all i create a string like this:
var child_id = "childId"+this.id;
alert(child_id);
the alert shows the correct string. this.id is a string and i also made a temp variable and put it again into a string (var tmp_child_id = child_id.toString()) to get sure it is a string.
after this i give this string to the function goToNextNode with a href link like this:
this.svgImageDiv.innerHTML = '<img src="img/transparent.png"/>
in the function goToNextNode i fire an alert at first and it dont show the string like the alert above, it says "[Object HTMLDivElement]".
function goToNextNode(childId){
alert(childId);
}
What happend to my string and how do i transport the string correctly?
EDIT:
SOLVED:
when i do var child_id = this.id; instead of var child_id = "childId"+this.id; and put the "childId" string to the id in the function goToNextNode() everything works. Dont ask me why it does behave like this, i dont get it...
END EDIT
You forgot the quotes for the argument inside the brackets:
this.svgImageDiv.innerHTML = '<img src="img/transparent.png"/>';
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']
I'm trying to add a variable within a new variable.
My first variable is:
var video5 = myObj.find('hosted-video-url').text(); (this returns a direct link to an mp4-file)
My second one should be something like:
var playvid5 = "playVideo('"video5"')";
Variable playvid5 should result "playVideo('http://link.to/video.mp4)')"
When I try to make variable playvid5 in the way I showed above, my whole code stops working and nothing is displayed. When I use var playvid5 = "playVideo('"+video5+"')";, the output is "playVideo('')", so that's not what I need either.
I'm trying to place the 2nd variable in this piece: ('Bekijk video')
In what way can I place the first variable in the second one?
Try to replace video5 string by its value.
var video5 = myObj.find('hosted-video-url').text();
var playvid5 = "playVideo('video5')";
playvid5 = playvid5.replace("video5", video5);
Why not just give the <a> tag an "id" value, drop it in the document, and then do:
$('#whatever').click(function() { playVideo( video5 ); });
Now, where you go to find the value, I don't think you've got the correct selector. Probably you need
var video5 = myObj.find('.hosted-video-url').text();
The "." before the string "hosted-video-url" is to select by class name. If "hosted-video-url" is an "id" and not a class, then you don't need to use .find(); you can select by "id" with $('#hosted-video-url').
Do you mean
var playvid5 = "playVideo('" + video5 + "')";
playvid5 will then be the string "playVideo('http://whatevervideo5is')
if video5 is blank then you will get "playVideo('')" so maybe that is the issue.
I have been trying for hours to fix this code, I can't see what's wrong:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal('+s_d+','+ddmmyy+')">'
The problem is in href="javascript ..."
s_d is a javascript variable defined as
var num = 2;
var s_d = "sname"+num;
var ddmmyy = "ddmmyy";
Basically I need to call a javascript function with different parameter each time.
Use a backslash like \'.
document.getElementById('detail'+num).innerHTML=
'<a class="dobpicker" href="javascript:NewCal(\''+s_d+'\',\''+ddmmyy+'\')">'
Since this is the value of a href attribute, HTML encode them:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal("'+s_d+'","'+ddmmyy+'")">'
Or better yet don't use the javascript: protocol:
[0,1,2,3,4,5].forEach(function(num) {
var s_r = "sname"+num;
var ddmmyy = "ddmmyy";
var aEl = document.createElement("a");
aEl.className = "dobpicker";
aEl.onclick = function() {
NewCal(s_d, ddmmyy);
}
document.getElementById('detail'+num).appendChild(aEl);
});
Your .innerHTML setting is using s_d, but your variable declaration has s_r.
EDIT: That was the first thing that jumped out at me. Having looked a bit closer and realised the values are strings, I think fixing the variable name together with adding some escaped quotation marks as in Daniel A. White's answer will do the trick.