We help IT Professionals succeed at work.

style.display = 'none' makes the error msg not display again

Camillia
Camillia asked
on
321 Views
Last Modified: 2013-01-07
I have a clear button and onClientClick, I call a javascript function. This is what's happening

1. I click the submit button and an error msg is displayed because the textbox is empty
2. I click Clear and the Javascript is called and the error msg is cleared
3. So far so good
5. I click Submit again, and the textbox is empty but this time, the error msg is not displayed.

I think it's because of style.display = 'none' that wont allow the error msg to be displayed again.

Anything else i can use besides style.display ='none'?


<script type="text/javascript">
      function clrCtrl() {

          document.getElementById("<%= ccNumber.ClientID %>").value = "";
          document.getElementById("<%= RequiredFieldValidator4.ClientID %>").style.display = 'none';
         
      }

   </script>

Open in new window

Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
'inline' and 'block' are the other two options.  You don't have to use that line at all, you can just leave the display setting alone.  What is it supposed to be doing for you?
CERTIFIED EXPERT

Author

Commented:
>>What is it supposed to be doing for you?
style.display = 'none'  clears the erorr msg is the first time. This is good.

in Step 5 above, when I click on submit button again when the textfield is empty...the error msg should show again but it doesnt. I think it's because the first time...style.display = 'none'
does something and the error msg is not displayed again...

I cant use "block'...i wasnt to clear the field but i dont want to clear it forever...
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
'block' is for things like <p> and <div> and 'inline' is for text and <span>.  Has nothing to do with clearing the field, only with how it is displayed.
CERTIFIED EXPERT

Author

Commented:
No, let me rephrase my question This is the literal I have


And an input field:

<input type="text" maxlength="16"   class="InputFields"  tabindex="1" runat="server" name="ccNumber" id="ccNumber" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator4" ControlToValidate="ccNumber" Text="Card Number is required." ErrorMessage="Card Number is required."></asp:RequiredFieldValidator>

Open in new window


And the button that calls the Javascript and the submit button

 <asp:LinkButton ID="btnCancel" CausesValidation="false"  OnClientClick="clrCtrl();return false;" runat="server" Text="Clear"  />

<asp:LinkButton ID="lbSubmit"  OnClick="lbSubmit_OnClick" CausesValidation="true"   runat="server" Text="Submit"   />

Open in new window


1. The first time when I click on the submit button without entering anything in the input box, the fieldvalidator error msg is displayed.

2. I click the clear button and this error is  cleared

3. I click the submit button again without entering anything in the input box (just like step 1), but this time, the fieldvalidator doesnt display the error msg. I think it's because of the style.display ='none' does something to that field. If I remove the javascript...the fieldvalidator's error msg gets displayed the second time (or whenever i click submit without entering anything in the input box)
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
do I need if-else in the javascript to display and hide?? i dont think so. For example:
User clicks submit and the error msg is displayed. User clicks clear and the error msg is cleared. User clicks submit again...at this point..the error msg should display again ...how can I change that Javascript to display the error msg? I have to check the value of the input box?
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
If you are going to call "clrCtrl()" both times then you do need the if-else and a variable to keep track of the state.  Otherwise, it can be two different functions.
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Tommy, yes..i think i had seen a solution like that somewhere..now i remember it. I'll try it. Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.