0

So i have something like 10 or more <input type=text> in which i fill them with database values by using php.

Some of the values will be 0 so the inputs will get their value set to 0. I want to select all those inputs and set their value to empty as the window loads.

I've did some googling but did not find any script that would help me do this.

4 Answers 4

5

Try this,

$(function(){
    $('input[type=text]').each(function(){
       if($(this).val()==0)
       {
           $(this).val('');
       }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

4

Or try this:

$(document).ready(function(){
   $('input[type="text"][value="0"]').val('');
});

Here it is working: http://jsfiddle.net/WPrNU/

1 Comment

@DaftDev Great! it is simple indeed!
3

Assuming you're using PHP to populate the values:

echo "<input type=\"text\" value=\"".($the_value ?: "")."\" />";

This will convert any falsy values (in particular 0) to the empty string.

5 Comments

Ah, but with that "better answer" the value will show as 0, and then suddenly disappear. That's very bad design.
That can occur, i agree but there is a better answer because your way i would have to change all inputs and many of the have diferent class, etc. More work would be needed and the "better answer" works fine for me.
So basically what you're saying is you're lazy and can't be bothered to do a simple Find+Replace to make your site just a little better?
no, i'm saying it works for me because all of the inputs i have are hidden and will only show up after an user event. So that gives more then enough time to run the script to set their value from 0 to empty. It's more simple and it wouldnt require any find+replace
Hmm... -narrows eyes- I guess that'll do, but still... if there's any error in the JavaScript it will show zeroes bright as day.
3

You have mentioned.

in which i fill them with database values by using php

The PHP code

 while ($row = mysql_fetch_assoc($result)) {
        if($row['your_column']==0){ $row['your_column']=''; };
    }

4 Comments

@DaftDev, Mine is safe and best , the other answers may not work 100% because, if you replace input type="text" with input type=" text" , all of the above code will fail.
No worries, why would i replace input type="text" with input type=" text"
@DaftDev, I didn't mean intentionally you do, what if you did accidentally ?
@DaftDev. Oh yea. you will. Peace !

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.