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 985735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:12:34+00:00 2026-05-16T05:12:34+00:00

Textbox refers to <input type=text> in this question. For each textbox with a given

  • 0

Textbox refers to <input type="text"> in this question.

For each textbox with a given class i would like to check if the value equals '', and if it does I want to make the value '0'. I would like to do this as soon as the page is loaded.

This is the closest I’ve got now:

$(document).ready(function() {
  if ($('.myclass').val() == '') $('.myclass').val('0');
})

The problem here is that this code will give all my textboxes (with class="myclass") the value '0' as soon as one of them is empty. I know this is the expected behaviour given the code I use now, but I’m pretty new to jQuery and I haven’t really got the hang of the selectors yet. How would I solve this? Do I need to create a separate function to do the check on one element and then call that for each of the elements that apply to the selector? If so, how would I do this?

  • 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-05-16T05:12:34+00:00Added an answer on May 16, 2026 at 5:12 am

    In jQuery 1.4, a function can be passed to val() that will be executed on all elements in the collection:

    $(document).ready(function() {
      $('.myclass').val(function (index, value) {
        // If the element has a value, return it, else return "0"
        return value || "0";
      });
    });
    

    Example

    For versions older than jQuery 1.4, you need to use .each() on the collection:

    $(document).ready(function() {
        $('.myclass').each(function() {
            this.value = this.value || "0";
        });
    });
    

    Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is there no tooltip text on errors? <Style TargetType={x:Type TextBox}> <Setter Property=Validation.ErrorTemplate> <Setter.Value>
i have coded a header file which contains a input textbox for search.this header
Silverlight renders few Chinese characters as Bold in a text box. This textbox has
In this example: <TextBox Name=textBox1 Height=23 Text=some text HorizontalAlignment=Left Margin=69,12,0,0 VerticalAlignment=Top Width=120 /> <Label
Lets say I have a custom data type that looks something like this: public
I would like to set the size of the dropdown same as the textbox
The TextBox control offers a MaxLength property, which allows the insertable text into that
I need a textbox that only allows an IP address. I can create this
Text in textbox and combobox do not render as they should be. Some letter
I have many TextBOX in asp.net which accepts the value from user and stores

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.