• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript not getting new value of text box

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
in my servlet i have introduced a text box that takes the value dynamically from a variable.
this has no issues and the text box is being updated.

however the javascript right after this textbox sticks to the first value that has been assigned to the text box.

the code runs in a loop, thus each time the percentage value is being updated.

any thoughts?



 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're asking a question about JavaScript, but the code you posted is Java code. It does generate JavaScript code, but now it's unclear whether you're asking about the generated Javascript or the posted Java.

And saying "the code runs in a loop" just adds to the confusion. Is it the Java code or the JavaScript code which runs in a loop?

And asking about the JavaScript which is "right after this textbox" is confusing too. What does "right after" even mean?

I'd suggest you start again. Try to clarify whether you are asking about the act of generating the JavaScript code, or whether you are asking about something which happens when you run that code.
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You're asking a question about JavaScript, but the code you posted is Java code. It does generate JavaScript code, but now it's unclear whether you're asking about the generated Javascript or the posted Java.

And saying "the code runs in a loop" just adds to the confusion. Is it the Java code or the JavaScript code which runs in a loop?

And asking about the JavaScript which is "right after this textbox" is confusing too. What does "right after" even mean?

I'd suggest you start again. Try to clarify whether you are asking about the act of generating the JavaScript code, or whether you are asking about something which happens when you run that code.



This is a snipet from my servlet.

There is a loop that generates a percentage value.

LOOP STARTS

The input textbox stores the percentage value.

This is done successfully.

Right after the text box, i have called a js where it gets the percentage value from the textbox.

LOOP ENDS.

The var in the js is not able to get the updated value of the textbox. However it sticks to the initial value that the textbox had in the first loop entry.

Meaning that in the first loop entry, the percentage is 1.
The textbox sets the value correctly so does the var in the js.
In loop entry 2 till end, the textbox updates the value correctly 2,3... but the var in the js sticks to value 1
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Alexwainy wrote:This is a snipet from my servlet.

There is a loop that generates a percentage value.


The code that you have posted will generate something that looks like this:Where is the loop?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Alexwainy wrote:This is a snipet from my servlet.

There is a loop that generates a percentage value.

LOOP STARTS

The input textbox stores the percentage value.

This is done successfully.

Right after the text box, i have called a js where it gets the percentage value from the textbox.

LOOP ENDS.



No, that isn't what happens. The Java code in your servlet runs and writes out that JavaScript code. (See Ron McLeod's post which shows you what is written.) Note carefully: it writes out that Javascript code. At some time later the JavaScript code appears in your browser, and the browser may cause it to be executed at that time.
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Mo Alexwainy wrote:This is a snipet from my servlet.

There is a loop that generates a percentage value.


The code that you have posted will generate something that looks like this:



Here right before the snipet

While(rs.next()){

percentage=cnt/couter*100;


}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pay attention to what Paul posted. The JavaScript code does not -- I repeat does not -- execute in line with the Java code. It is run much later in the browser on the user's machine.

Please read this article -- even though it talks about JSP (why aren't you using JSP?) the same is true for HTML code emitted by a servlet.
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Pay attention to what Paul posted. The JavaScript code does not -- I repeat does not -- execute in line with the Java code. It is run much later in the browser on the user's machine.

Please read this article -- even though it talks about JSP (why aren't you using JSP?) the same is true for HTML code emitted by a servlet.



Thanks. But why does the alert within this js runs at the same time the loop is executed?
Doesnt this mean that the js runs every time the loop circulates?
 
Mo Alexwainy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Alexwainy wrote:

Bear Bibeault wrote:Pay attention to what Paul posted. The JavaScript code does not -- I repeat does not -- execute in line with the Java code. It is run much later in the browser on the user's machine.

Please read this article -- even though it talks about JSP (why aren't you using JSP?) the same is true for HTML code emitted by a servlet.



Thanks. But why does the alert within this js runs at the same time the loop is executed?
Doesnt this mean that the js runs every time the loop circulates?

Also, the below js runs anytime in my servlet whenever i execute it. It does not run at anytime later on.

out.println("<script type=\"text/javascript\">");
out.println("document.getElementById('myBody').innerHTML = '';");
out.flush();

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Alexwainy wrote:
Thanks. But why does the alert within this js runs at the same time the loop is executed?


It doesn't. You are writing out the same markup multiple times. Do a View Source at the browser to see what it is that you are sending to it.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Alexwainy wrote:Also, the below js runs anytime in my servlet whenever i execute it. It does not run at anytime later on.

out.println("<script type=\"text/javascript\">");
out.println("document.getElementById('myBody').innerHTML = '';");
out.flush();



No, it doesn't. The Java code which you posted there of course runs every time you execute the servlet. It generates that JavaScript code and sends it as part of the response.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic