JavaScript access javascript variable value in jsp access javascript variable in html,access javascript variable in php,

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.

Answer:1

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);
});
Answer:2

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

Answer:3

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

Answer:4

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!

Answer:5

Can I set the biggest width and height without video cutting and page scrolling? <style> video { width: ?; height: ?; } </style>

Can I set the biggest width and height without video cutting and page scrolling? <style> video { width: ?; height: ?; } </style>

  1. 100 width height iframe
  2. 100 width height background image
  3. 100 width height div
  4. width 100 height auto
  5. width 100 height auto react native
  6. width 100 height proportional
  7. width 100 height
  8. width 100 height auto not working
  9. width 100 height square
  10. width 100 height 100vh
  11. 100(width) * 120(height) pixel
  12. textarea 100 width height
  13. 100 width and height css
  14. 100(width) * 120(height) pixels online
  15. svg 100 width height
  16. iframe width 100 height auto
  17. img width 100 height auto
  18. max-width 100 height auto
  19. background width 100 height auto
  20. image width 100 height auto

I need to get the html of a p tag, however there is a br tag inside of it. So it looks like: <p> Some Text <br> Some More Text </p> I need to get the first part and ...

I need to get the html of a p tag, however there is a br tag inside of it. So it looks like: <p> Some Text <br> Some More Text </p> I need to get the first part and ...

  1. jquery replace part of innerhtml

How do you testing angular controller with external library usage such as google analytic event tracking. For example: $scope.showVolumn = function() { ga('send', { 'hitType': 'event', ...

How do you testing angular controller with external library usage such as google analytic event tracking. For example: $scope.showVolumn = function() { ga('send', { 'hitType': 'event', ...

  1. angular testing with jest
  2. angular testing with protractor
  3. angular testing with jasmine
  4. angular testing with cypress
  5. angular testing with selenium
  6. angular testing with karma
  7. angular testing with mocha
  8. angular testing with observables
  9. angular testing with cucumber
  10. angular testing with puppeteer
  11. angular testing with material
  12. angular testing with httpclient
  13. angular testing with router
  14. angular testing with @input
  15. angular testing with services
  16. angular testing with mocks
  17. angular testing with phantomjs
  18. angular testing with headless chrome
  19. angular testing with activatedroute
  20. angular testing with testbed

I'm trying to accomplish the effects on this site, but I only need to move around a few things based on scrolling. My goal is for the basketball to go into the box, then after scrolling down a little ...

I'm trying to accomplish the effects on this site, but I only need to move around a few things based on scrolling. My goal is for the basketball to go into the box, then after scrolling down a little ...

  1. jquery move element to another div
  2. jquery move element up
  3. jquery move element to new parent
  4. jquery move element after another
  5. jquery move element before another
  6. jquery move element position
  7. jquery move element out of parent
  8. jquery move element to top
  9. jquery move element on scroll
  10. jquery move element left
  11. jquery move element to body
  12. jquery move element up one level
  13. jquery move element before sibling
  14. jquery move element to end of container
  15. jquery move element to top of list
  16. jquery move element to end of parent
  17. jquery move element above sibling
  18. jquery move element to top of parent
  19. jquery move element on page
  20. jquery move element above another