D-Lef
D-Lef

Reputation: 1349

access javascript variable value in jsp

I want to pass a javascript variable to my servlet, where I need to use it.

In javascript, the variable count returns the rows of my table and I can show count in the jsp, using $('#counter').html(count); , but I cannot pass count's value to my servlet. I tried document.getElementById("hiddenField").value=count; but it doesn't work.

Javascript

<script>
    var count = 3;
    $(function() {
        $('#counter').html(count);
        $('#addButton').bind('click', function() {
             count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
        $('#deleteButton').bind('click', function() {
            count = document.getElementById("dataTable").getElementsByTagName("tr").length;
            $('#counter').html(count);
        });
    });
    document.getElementById("hiddenField").value=count; // ???
</script>

JSP

Count: <span id="counter"></span> <%-- it works --%>

<form method="post" action="newteamsubmit"> 
...
<input type="hidden" id="hiddenField" name ="countRows" />
<input type="submit" name ="button1" value=" Submit " />
<input type="submit" name = "button1" value=" Cancel " />
</form>

Servlet

String cr = request.getParameter("countRows"); //I' ve tried also to convert it 
// to int, but that's not my problem, since I cannot pass the value as a start

I've spent many hours, trying to figure out how I can access a javascript variable in jsp, but I haven't found any solution. Thanks in advance.

Upvotes: 3

Views: 13337

Answers (5)

UVM
UVM

Reputation: 9914

You can use this way:

document.forms[0].countRows.value = counter

Hope this will help you

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692271

The count is computed each time the add button or the delete button are clicked. But you only set the hidden field value once, when the page is loaded (and its value is thus hard-coded to 3).

You must set it, as you're doing for the #counter element, in your click handlers:

$('#addButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});
$('#deleteButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

Also note that you're repeating exactly the same code in two click handlers here. You should do that only once, for the two buttons:

$('#addButton, #deleteButton').bind('click', function() {
    count = document.getElementById("dataTable").getElementsByTagName("tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

or even, since you're using jQuery:

$('#addButton, #deleteButton').bind('click', function() {
    count = $("#dataTable tr").length;
    $('#counter').html(count);
    $('#hiddenField').val(count);
});

Upvotes: 1

Canis
Canis

Reputation: 4440

The main issue here is that you are trying to access from the server, a variable that only exists at the client. To access that variable you have to send it from the client to the server using AJAX to trigger some form of API in the backend. REST, SOAP or XML-RPC are common technologies used for this sort of thing. The server side code is used for generating the UI and providing it with data from a database or such. Commonly the UI is generated only once, and then the client calls the server asking for more data in response to user actions, like clicking a button.

Imagine a table filled with information about books: title, author, publish date etc. This table can get quite large, and traditionally this table will be split up over several pages and possibly a dynamic filter. To save bandwidth and increase the user experience by not loading the entire page from scratch you can use AJAX to ask the server for just the relevant data. Doing so the page updates dynamically and smoothly for the user.

In your case, you can use this technique to update the server every time the user clicks the button.

If however you are really just looking to update a hidden field in a form with a value as the user performs actions, and the server wont do anything with it except show it you can just use javascript.

Remember also that the request variable contains the data you post to the server when you submit the form. The servlet will get the data after the client has posted it, which is after the JSP has generated the page. The sequence of the code execution is JSP -> Javascript -> Servlet.

Hope this helps!

Upvotes: 0

Vivek Vermani
Vivek Vermani

Reputation: 2014

Make sure of 2 things -

  1. There is only one element with id "hiddenField" on your page.
  2. Make sure that the following code

document.getElementById("hiddenField").value=count;

is after in the page.

Just make sure that js sets the hiddenField after the element has been loaded. 3. check for any JS errors using Javascript console.

Rest it looks good

Upvotes: 0

AbhinavRanjan
AbhinavRanjan

Reputation: 1646

document.getElementById('hiddenField').value is not set because it is outside your document.ready. Put it inside your click handler.

Upvotes: 0

Related Questions