0

In my jsp file I generate dynamically multiple input tags, trough a database. I'd like to know the values of each. I've tried doing it in Javascript, but according to some answers in this website this is not possible. Example:

<input type="number" id="age" class="v">
<input type="text" id="name class="v">
...

And on the jsp side I'd like to get:

"age" => 18
"name" => "Joe"

Any ideas on how to achieve this?

Edit

In case you are wondering, my Javascript is fairly simple, I can get all the values I need simply by doing:

var chars = document.getElementsByClassName("v");

EDIT 2 My (simplified) JSP looks something like this:

<%= session.getAttribute("chars")%>
    <form action="hello" method="POST">
    <c:forEach items="${chars}" var="ch">
        <input type="number" id="${ch}" class="v">
    </c:forEach>
    <input type="submit">
    </form>

"chars" is an array that was created through calls to the database, this array is displayed and created dynamically. So what I want to do is pass all those values, like ("age" => 18) to another my hello servlet, so I can work on that info. For example if the value of the inputs is something like this: //ID value "age" => 18 "name" => "Joe"

On hello I should have access to that.

Safirah
  • 365
  • 6
  • 17
  • I honestly have no idea what you are trying to do. " I'd like to know the values of each. " What does that mean? Take a step back and try to explain what it is actually what you want to do preferably with example code. Also: I don't see any JSP code. – wvdz Jan 24 '17 at 18:31
  • @wvdz, I'm sorry for not explaining myself very well, does this help? – Safirah Jan 24 '17 at 19:31

2 Answers2

1

Using pure JavaScript you can get the field values using querySelector which allows you to retrieve properties from multiple elements with the same class identifier

document.querySelector('.v').value;

You will then have access to both field values via js. To read those values using jsp you the JavaScript will need to be included within the same file. You can do something like:

JSP:

You will need to add the HTML Name="myFieldName" to the input fields.

<%= request.getParameter("myFieldName") %>
coletrain
  • 2,809
  • 35
  • 43
0

If you are in a form, you could use a POST method to submit the datas of the different inputs inside your form.

Here is a previous post on StackOverlow that might help you get the datas you want :

How can I get form data with JavaScript/jQuery?

Community
  • 1
  • 1
Sirmyself
  • 1,464
  • 1
  • 14
  • 29