1

If the user did not give any value to text box the value 0 (zero) to be set on blur but the text box should not display the value How can i do it using jquery

<g:textField name="distance" id = "distance" value="${tripInstance?distance}" min="0" class="number" />

2 Answers 2

1

You can't as "value" is the display value.

Only way you be able to do it is intercept the form submission and prepopulate with default values if none are entered.

Failing that, the best place to hand it would be at the server end, ultimately where this kind of thing should happen.

That said, you should never rely on JavaScript to set/enforce default values as the user may not have JavaScript enabled or worse they are manipulating before sending it to you.

In that respect JavaScript should be considered dirty.

On second thoughts, you could do this:

<input type="hidden" name="distance" id = "disatance_default" value="0" />
<g:textField name="distance" id = "distance" value="${tripInstance?distance}" min="0" class="number" />

I think, if nothing was entered into the "textField" it wouldn't get sent back to the server, and therefore the hidden input field with the same name would take precedence.

The last most field of that name (which contains a value) will always be the one that is sent back to the server.

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

Comments

1

I dont see a reason for doing this. why do you need to set it to 0 and not display it.. if you need to do some prcessing on this value then check if this value is set or not. if it is not then select 0.

Comments

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.