0

I have a question regarding how to access an index from a model attribute list dynamically. In my code, I have some javascript which is reading a value from a model. The model has an attribute which is potentially a list.

document.getElementById("phoneNumberRPhone").value = "${model.people[index].phoneNumber.number}";

Here, you can see that I'm trying to set a javascript value to a number retrieved from a model where I can have multiple people. Index is my dynamic value. It works fine if I specifically state model.people[0] or model.people[1], but if I try to set a number to index and use index dynamically, it no longer works.

I would be very grateful for any help anyone could provide on this. I'm certain it's either just a matter of user error or improper use of syntax.

1 Answer 1

2

Apparently ${index} doesn't exist at all in the JSP/EL scope at the point JSP/EL has to print that piece of JS code. It would only work of you're doing for example (although this approach is highly questionable):

<c:forEach items="${model.people}" varStatus="loop">
    document.getElementById("phoneNumberRPhone").value = "${model.people[loop.index].phoneNumber.number}";
</c:forEach>

Keep however in mind that JSP is merely a HTML code generator and that JavaScript is part of it. JSP and JavaScript doesn't run in sync. Rightclick page in webbrowser and do View Source to see it.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer! I'd considered using foreach, but that seemed a really odd approach to have to loop through the list every time I wanted to select a new value. Maybe that's the only way to do it with the way I have it coded.
Yes, as mentioned, this approach is questionable. It's more likely that you're actually looking for the solution in completely the wrong direction. If you elaborate more about the functional requirement for which you thought that this is the right solution, then we may be able to propose the right solution. Again: JSP is a HTML code generator. All the webbrowser retrieves is one and all HTML code along with some JS. JSP doesn't run in webbrowser, but in webserver. JS runs in webbrowser, not in webserver.
It could be that I need to attack this from another angle. Here's the context. I have a client name that I've entered into a web form. I submit the form and retrieve some phone numbers. I'm adding the phone numbers returned along with the person names attached as the people attributes of the model. I then click on the data on the page and want to select that particular person for reuse elsewhere. However, the number of people/numbers that can be returned is dynamic.
Is it absolutely necessary that you need JavaScript for this instead of just a simple form submit to the server, if necessary by ajax?
Yes, you could do it that way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.