0

I'm working with PHP+MySQL. In one page I have a form with aprox. 30 inputs.

After showing the form, at the end I execute some jQuery sentences styling the form.

Well, some rows in my SQL have data empty inputs, so I show empty inputs. I want to fill the form with "------" without writing that slashes into DB because makes SQL much bigger.

I'm trying "replaceWith", but i dont know if this implementation with "empty" is correct. This does not work, even without .is(:empty)

<script>
    $('#form').find('input[type=text]').is(':empty').replaceWith(function()
       {
        return this.val('------');
       });
</script>
5
  • 3
    For one thing you can't return return. Commented Aug 27, 2014 at 14:29
  • 1
    I would suggest doing this server-side instead. You can simply format the output before returning it with a simple IF without touching the DB, like if(is_null(field)) {field = "----";} Commented Aug 27, 2014 at 14:30
  • Missing quotation marks around :empty Commented Aug 27, 2014 at 14:38
  • @JaredEitnier fixed! Sorry, i had not that written in my file :) Commented Aug 27, 2014 at 15:19
  • @DJDavid98 Fixed! That really didn,t know. Thank you Commented Aug 27, 2014 at 15:20

3 Answers 3

3

jQuery's :empty finds elements with no children, not elements without a value, and as an input can't have children it finds nothing.

And jQuery's is() returns a boolean, not a collection.

jQuery's replaceWith replaces the entire element, not just the value.

Check the value instead.

$('#form input[type=text]').val(function(_, val) {
    return val.length === 0 ? '------' : val;
});

or

$('#form input[type=text]').filter(function() {
    return this.value.length === 0;
}).val('------');

Throw in a $.trim to trim off whitespace if needed

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thank you. Obviusly I misunderstood the function of empty. Unluckily that two options is not working. I gonna try with $('#form').find('input[type=text]')
It's not just jQuery's :empty, it's the way that CSS selector is supposed to work.
2

Use something like this:

<script>
    $('#form').find('input[type=text]').each(function(e){
        if($(this).val()=="") $(this).val("---");
    })       
</script>

1 Comment

That works! as @adeneo write, I need to use .trim to trim off the whitespaces. Thankyou !!!!
1

Have you considered using the HTML placeholder attribute?

You could use placeholder to make the dashes appear in light grey text without actually changing the value of the input. They would automatically be added to any blank fields on your page:

<input type="text" placeholder="----" value="asdf" name="notempty"
    title="This has a placeholder, but doesn't show it because the input is filled with something" />
<input type="text" placeholder="----" value="" name="empty"
    title="This is empty, so it shows the dashes" />

The placeholder text would not be included in your $_POST data. In this example, $_POST['empty'] would be blank.

The placeholder attribute is supported by many of the modern browsers, but not IE9:

http://caniuse.com/#feat=input-placeholder

There are various jQuery plugins that imitate this effect as well. You can read about a few of them here:

How do I get placeholder text in firefox and other browsers that don't support the html5 tag option?

1 Comment

That it is a really good option but I could not use that because after adding the slashes I use other Jquery sentence to convert this inputs to plain text, so if there is no text written in the input, the conversion makes it dissapear

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.