Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:06:53+00:00 2026-06-16T07:06:53+00:00

I am trying to clear all the fields in my .aspx page using javascript

  • 0

I am trying to clear all the fields in my .aspx page using javascript (Should be cross-browser). The below code is working fine for TextBox fields but not for Label fields.

var elements = document.getElementsByTagName("input");
for (var i = 0; i < elements.length; i++) {
    if (elements[i].type == "text") {
        elements[i].value = "";
    }
    else if (elements[i].type == "label") {
        elements[i].value = "";
    }
}

Later I saw that HTML is rendering asp.net Labels as span so I also tried:

else if (elements[i].type == "span") {
    elements[i].innerHTML = "";
}

Still the Labels are not being cleared. Am I doing something wrong here?

And the other problem is, whenever I refresh the page, the cleared TextBox fields are again being populated with the old values.. (really frustrating)

I am trying the above code by referring this

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:06:54+00:00Added an answer on June 16, 2026 at 7:06 am

    In modern browsers, this will clear all span elements.

    [].slice.call(document.querySelectorAll("span")).forEach(function(e){
        e.innerHTML="";
    })
    

    If you have applied the class “label” to your ASP labels, then you could be more specific:

    [].slice.call(document.querySelectorAll(".label")).forEach(function(e){
        e.innerHTML="";
    })
    

    Here is an example that will work in older browsers:

    var spans = document.getElementsByTagName("span");
    for (var i=0; i < spans.length; i++)
    {
        if ("label" == spans[i].className)
            spans[i].innerHTML = "";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make my code more clear using the include function of
Hi im trying to create a button to clear all form fields and a
I'm having troubles trying to clear all fields from a form after Ajax success.
I am trying to clear images using external jquery file but it's not showing
I'm trying to first clear the class of all options and then add a
I'm trying clear the value of a lookup field via Javascript. I've tried this:
I'm trying to create an HTML form (using JSP) which contains Javascript buttons (rather
I would like to clear all inputs, selects and also all hidden fields in
I'm trying to clear one column for all records in my table. For example,
I am trying to clean up a ByteBuffer to be all zero bytes (all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.