user2544750
user2544750

Reputation: 1

mixing javascript with java

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

Upvotes: 0

Views: 1370

Answers (3)

Jafar Ali
Jafar Ali

Reputation: 1114

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.

Upvotes: 0

Keith
Keith

Reputation: 4184

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.

Upvotes: 2

user2357112
user2357112

Reputation: 281330

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

Upvotes: 1

Related Questions