1. ホーム
  2. javascript

[解決済み] jQuery check if <input> exists and has a value

2023-06-20 07:33:57

Question

I have the following example:

<input type="text" class="input1" value="bla"/>

Is there a way to check if this element exists and has a value in one statement? Or, at least, anything shorter then

if($('.input1').length > 0 && $('input1').val() != '')

Just getting frustrated here with mile-long conditions.

How to solved?

The input won't have a value if it doesn't exist. Try this...

if($('.input1').val())