Writing letter directly behind a value [duplicate] - javascript

This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
I need to assign a time to a profile value from our database, but I don't know how to seperate the values.
It is the following code:
var pauze_datum = new Date("2020-01-27T10:33:00");
The time should be assigned to var Temporary_Date which is a filled in date in a form. How do you seperate the letter e and T?
var pauze_datum = new Date("Temporary_Date T10:33:00");

You can use either regular concatenation:
var value = "2020-01-27";
var pauze_datum = new Date(value + "T10:33:00");
or template literals:
var value = "2020-01-27";
var pauze_datum = `${value}T10:33:00`;

Related

How to use a JavaScript value inside EJS [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 1 year ago.
How can I use a JavaScript value inside an EJS statement?
E.g. I grab the value of a select option with JavaScript and want to load data from an object depending on the value.
var gerichtSelectID = $("#gericht" + id).val(); //e.g. 8
var gerichtPreisID = <%= gerichte.data[gerichtSelectID].preis_id %>; // should be gerichte.data[8].preis_id
EJS values can't be modified in JavaScript, but the easiest solution is to convert the EJS object into JSON.
var gerichte = <%- JSON.stringify(gerichte) %>;
var gerichtSelectID = $("#gericht" + id).val();
var gerichtPreisID = gerichte.data[gerichtSelectID].preis_id;

javascript using an variable for key of object [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am making a shopping cart via javascript/jQuery and I am using localStorage. However, I am running into a problem setting the key value of an object. I created this function, but it has not been successful:
function update_aantal(e){
var value = $(e).parent().find(".form-aantal-val");
var size = $(e).closest('td').attr('class');
console.log(size);
valuepush = {
size: value.val()
};
var cart = JSON.parse(localStorage.getItem("src"));
$.extend(cart[0], valuepuch );
localStorage.setItem("src",JSON.stringify(cart));
}
The console.log(size) shows the correct value that I want to change/add into the localStorage but the size in the object valuepush says "size" when the function is complete and the value is added.
How can I pass the size as an object so that the value of size is stored?
var valuepush = {};
valuepush[size] = value.val();

Issue getting my objects properties [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.
Here is my object.
var restaurantOrder = {
"my entree": "cheeseburger",
"my side": "fries",
"the drink": "water"
};
I would like to get the value of entree however nothing I am trying seems to work :(
Here is what I have tried.
var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];
None of the above lines work :( Thank you for your help.
Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.
Also, because of the spaces you will need to enclose the property name with quotes, so either:
var entreeValue = restaurantOrder["my entree"];
or
var entreeValue = restaurantOrder['my entree'];
will work.

Creating JSON without the use of any external library? [duplicate]

This question already has answers here:
Encoding Javascript Object to Json string
(2 answers)
Closed 6 years ago.
SORRY! EDIT ON OLD BROWSERS prior to ie8!
In order to create XML on the fly in JS, one can do the below,
Is there any way I can achieve the same - creating JSON on the fly in JS without the use of any external library?
var parent = document.createElement("parent");
var children = document.createElement('children');
var child1 = document.createElement('child1');
var child2 = document.createElement('child2');
var textNode1 = document.createTextNode("some text1");
var textNode2 = document.createTextNode("some text2");
child1.appendChild(textNode1 );
child2.appendChild(textNode2 );
children.appendChild(child1);
children.appendChild(child2);
parent.appendChild(children);
alert(parent.outerHTML);
Just use JSON.stringify()
i.e.
var o={a:12};
JSON.stringify(o);
results in
"{"a":12}"

Check if a string contains a specific number in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: string contains
I want to check if a string contains a specific number in JavaScript, how can I rewrite the statement s contain d in below code?
var s = '11/14/2012';
var d = '14';
if (s contain d) {
//...
}
My case is similar to Check if a string contains a certain number, but how can I implement this action in JavaScript?
Thanks
The easier way is to use index Of.
if(s.indexOf(d) > -1)
var s = '11/14/2012';
var d = '14';
if (s.match(d)) {
alert('Found');
}​

Categories

Resources