0

I have a page containing form fields with menus. on clicking a button, the form fields should be validated and errors should get displayed in a row of table on a Modal window.

I have declared a global variable to capture errors of different field. Global variable is a string and the error gets added as rows as shown below

first I initialize a global variable var gErrorTableRow = "";

The same variable keeps get append with table row based on errors of particular field name like shown below.

gErrorTableRow += '<tr><td>'+sFieldName+'</td><td>Enter Non Zero Value</td></tr>';

the table row shall get displayed on a Modal window on clicking a button.

But the variable gErrorTableRow is getting append with errors again and again even after i re initialize the if variable 'gErrorTableRow = "" ;' after modal window show., on clicking the button.

How to re initialize this gErrorTableRow variable without refreshing the page.

I re edited based on the comments, hope i am clear now.

3
  • just assign new value to the variable inside the attached event handler... Commented Jun 11, 2016 at 7:38
  • 2
    What has jQuery to do with this? Commented Jun 11, 2016 at 7:38
  • I think perhaps we're missing the question. Have a read of How do I ask a good question? and see if you can edit the question to make it more clear. Unless, of course, the answer below does answer it. Commented Jun 11, 2016 at 7:39

3 Answers 3

2

You simply run code assigning it a new value, for instance:

gErrorTableRow = "";
Sign up to request clarification or add additional context in comments.

Comments

0

Seems the problem is with the + in this following line

The + operator can also be used to add (concatenate) strings.

gErrorTableRow += '<tr><td>'+sFieldName+'</td><td>Enter Non Zero Value</td></tr>';

You can simply try by

 gErrorTableRow = '<tr><td>'+sFieldName+'</td><td>Enter Non Zero Value</td></tr>';

1 Comment

I want the variable gErrorTableRow getting append with error information of different types of form data. I like to re initialize this variable once the error are shown in modal window. the problem i face is this statement gErrorTableRow = ""; or gErrorTableRow =null is not getting the variable reinitialized.
0

Instead of a global variable, add the errors to their container directly. When the user clicks the button, empty the container and reinsert all validation errors again.

Something like this:

function onButtonClick() {
    var errorContainer = $('#error-container');
    $(errorContainer).empty();
    var errors = validateInput();
    $.each(errors, function (index, error) {
         var row = $(document.createElement('tr'));
         var fName = $(document.createElement('td'));
         $(fName).text(sFieldName);
         var eText= $(document.createElement('td'));
         $(eText).text(error.Text);
         $(row).append(fName);
         $(row).append(eText);
         $(errorContainer ).append(row);
    });
}

The above code assumes that you have a method to validate input and that it returns a collection of errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.