0

I need to create a jsp form with javascript. The problem is to assign the data path.

Imagine $form.work = "Hello Work";

If I create the form with html:

<div id='workForm'> Work:<input value=${form.work}></input> </div>

The input will show the work value (Hello Work). But I need to create this div form with js, something like:

var oDiv = document.createElement('div');
oDiv.setAttributte('id', 'workForm');
var oInput= document.createElement('input');
oDiv.appendChild(oInput)
...

I tryed to add an attribute to the input like:

oInput.addAttributte('value', '${form.work}')

But it shows: ${form.work} not Hello Work

Can someone help me?

Thanks!

Pablo Garcia
  • 361
  • 6
  • 23
  • Check this [Reading a JSP variable from JavaScript](https://stackoverflow.com/questions/4803906/reading-a-jsp-variable-from-javascript) – HardExploder Aug 03 '18 at 13:52

2 Answers2

1

Try this,

var work = "<%=form.work%>"; 

or

var work = "${form.work}"
alert(work);

If the window/ System alert had provided what you need, then you can setAttribute("value", work) to the hidden field or the JSP element you needed.

薛源少
  • 306
  • 1
  • 6
  • 18
0

You're close, you must use template literals (backticks) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

oInput.setAttribute('value', `${form.work}`)
t3__rry
  • 2,817
  • 2
  • 23
  • 38