How to use a JavaScript value inside EJS [duplicate] - javascript

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;

Related

Writing letter directly behind a value [duplicate]

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`;

how to select a value from drop down using jquery that got generated from txt file [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 5 years ago.
I am generating a drop down from a text file using "load" function in jquery. I am in need to select a dynamic value from this drop down based on my session variable.
$("#id").val(session variable) is not working.
Is there any other way to select value from this drop down?
you can use a scriptlet to transfer the session variable to javascript like so:
var sessVar = '<%= session.getAttribute("something") %>';
Then you can do the following to get the value:
var id = "#" + sessVar;
var dropValue = $(id).val();
and to set a value you do:
$("#id").val(sessVar);
lets suppose your Select dropdown id is : "dropdownlist1"
Use :
var SelectedValue = $('#dropdownlist1').val();
To Assign value using jquery:
$('#dropdownlist1').val("SomeText");

How to retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

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}"

Parse JSON in Ext JS 4 or JavaScript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 10 years ago.
I have this type of JSON:
{"value":[{"idProductCategoryAttributeValue":43,"value":"7","sortOrder":0}]}
I want the individual parameters values like. How do I parse this JSON string in Ext JS 4 or in simple JavaScript?
Have a look at http://docs.sencha.com/ext-js/4-1/#!/api/Ext.JSON. There you find how you can parse JSON with Ext JS 4.
var strJson = '{"value": [{"idProductCategoryAttributeValue":43,"value":"7","sortOrder":0}]}';
var obj = Ext.JSON.decode(strJson);
var obj = Ext.decode(jsonstr);

Categories

Resources