Skip to main content
Code Review

Return to Question

Make title describe what code does per site convention - see https://codereview.stackexchange.com/help/how-to-ask
Link

Looking to optimize this short JavaScript, so my colleagues don't laugh at me. The code enables Enable a textbox when the previous one is no longer empty

I`veI've written this JavaScript code, which function is to disabledisables textboxes if the previous textbox is empty. This is so people enter the text in order - write first in the first textbox, only then the second textbox enables, write in the second, enable the third, etc. It also clears and disables any textboxes bellowbelow the one that had it'sits text deleted.

The CODE WORKScode works as intended, but I`mI'm afraid if my colloguescolleagues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");```

I`ve written this JavaScript code, which function is to disable textboxes if the previous textbox is empty. This is so people enter the text in order - write first in the first textbox, only then the second textbox enables, write in the second, enable the third etc. It also clears and disables any textboxes bellow the one that had it's text deleted.

The CODE WORKS as intended, but I`m afraid if my collogues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");```

I've written this JavaScript code, which disables textboxes if the previous textbox is empty. This is so people enter the text in order write first in the first textbox, only then the second textbox enables, write in the second, enable the third, etc. It also clears and disables any textboxes below the one that had its text deleted.

The code works as intended, but I'm afraid if my colleagues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");
added 85 characters in body
Source Link

I`ve written this JavaScript code, which function is to disable textboxes if the previous textbox is empty. This is so people enter the text in order - write first in the first textbox, only then the second textbox enables, write in the second, enable the third etc. It also clears and disables any textboxes bellow the one that had it's text deleted.

The CODE WORKS as intended, but I`m afraid if my collogues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");
```

I`ve written this JavaScript code, which function is to disable textboxes if the previous textbox is empty. This is so people enter the text in order - write first in the first textbox, only then the second textbox enables, write in the second, enable the third etc.

The CODE WORKS as intended, but I`m afraid if my collogues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");
```

I`ve written this JavaScript code, which function is to disable textboxes if the previous textbox is empty. This is so people enter the text in order - write first in the first textbox, only then the second textbox enables, write in the second, enable the third etc. It also clears and disables any textboxes bellow the one that had it's text deleted.

The CODE WORKS as intended, but I`m afraid if my collogues see it they will laugh at how it is written and the more textboxes it has the more spammy it gets. Please tell me how I can optimize this?

$q.find(".row1 :text, .row2 :text, .row3 :text").on("keyup", function(){
 var userRow1 = $q.find(".row1 :text")
 var userRow2 = $q.find(".row2 :text")
 var userRow3 = $q.find(".row3 :text")
 var userRow4 = $q.find(".row4 :text")
 
 if (userRow1.val().trim().length > 0) {
 userRow2.enable(); 
 } else {
 userRow2.val("").disable().fclear;
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow2.val().trim().length > 0) {
 userRow3.enable(); 
 } else {
 userRow3.val("").disable().fclear;
 userRow4.val("").disable().fclear;
 };
 
 if (userRow3.val().trim().length > 0) {
 userRow4.enable(); 
 } else {
 userRow4.val("").disable().fclear;
 };
 
}).trigger("keyup");
```
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /