0

basically my problem is when I want to mix javascript with java code since I did not take the variable (var nombreRodamiento javascript) when I put the "<%" to start putting java code. please note the bold line, which is what the compiler does not like.

<script type="text/javaScript">
function moveToRightOrLeft(side) {
    var listLeft = document.getElementById('selectLeft');
    var listRight = document.getElementById('selectRight');
    if (side == 1) {//izquierda
        if (listLeft.options.length == 0) {
            alert('Ya aprobaste todos los items');
            return false;
        } else {
            var rodamientoElegido = listLeft.options.selectedIndex;
            var nombreRodamiento = listLeft.options[rodamientoElegido].text;
            move(listRight, listLeft.options[rodamientoElegido].value,
                    listLeft.options[rodamientoElegido].text);
            listLeft.remove(rodamientoElegido);
            <%
            **String nombreRodamiento = '%> nombreRodamiento;<%'**
            for (int i=0;i<listaItems.size();i++){
                if (listaItems.get(i).equals(nombreRodamiento))
                    listaItems.remove(i);
            }
            %>
            if (listLeft.options.length > 0) {
                listLeft.options[0].selected = true;
            }
        }
    }
}
</script>

Regards

1
  • 1
    should not this String nombreRodamiento = '%> nombreRodamiento;<%' be String nombreRodamiento = "%> nombreRodamiento;<%"; ? Commented Jul 3, 2013 at 2:25

3 Answers 3

2

Assuming that this is all inside of a JSP. The java code (scriptlet, everything inside the <% %> tags) will execute server side, and the javascript will execute client side (in the user's browser). Yet you seem to be assigning a java variable the value of a javascript variable, nombreRodamiento. That is not going to work. The javascript is just text, with no values, execution context, etc, whenever the scriplet is being evaluated.

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

Comments

1

Java strings require double quotes, and you're missing a semicolon, which Java will not automatically insert.

4 Comments

it says them <code> An error occurred at line: 27 in the jsp file: /jsp/Cotizacion/AprobarCotizacion.jsp String literal is not properly closed by a double-quote 24: listLeft.options[rodamientoElegido].text); 25: listLeft.remove(rodamientoElegido); 26: <% 27: String nombreRodamiento = "%> nombreRodamiento;<%"; </code>
The %> in the string literal might be treated as ending the embedded Java. You may have to escape it somehow.
Guys the line is teying to assign a value from client side to a variable. on server side. what are you discusing about. its not possible. if you need any solution then plz explain what you want to achive.
I have no idea how the Javascript and the Java interact here. I'm just addressing syntax errors. You should probably be commenting on the question instead of this answer.
0

Assuming this is a part of jsp file java code and js code executes separately. first java code get execute on server side and then the javascript code and that is on client side. Infact the java scriplet in jsp renderes the js code to be executed later on client which in this case is a browser.
Hence one cannot assign javascript variable value to a java variable but the reverse is possible.T

Edit:

I can give you the steps as I dont know your server side implementation.

  1. render the page. You must be rendering the list by some type of array or equivaletn object on server side. save both right and left side element on session.
  2. when the list box do some element exchange. exucute your js code.
  3. you have to update the same on server side so send the selected element index and side information to server side using ajax.
  4. update the server side list objects in the session accordingly. Update db if needed.
  5. From next time render this list box suing this objects on ths server side sessions.

Hope this helps.

6 Comments

and then how I can fix it?
I need pass the contents of the listbox (select with many options) to an element of type List <String>. just that. and I want to do in the javascript I'm detailing because that is where there is movement of a listbox to another.
Why you want the value fron javascript to java? can't you achive what you want through javascript alone.
You want to update java side that the option has be moved from one listbox to another. you can save the value of list box on serverside session and update it using ajax.
Next time you want to render the list box render them from this valies from session. Hope this solve your problem.
|

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.