Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript create required filed with jquery

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #151788
    amis
    Participant

    hey there

    I am newbie in jquery and i want create required filed with it

    i make this

    $(function(){
    
        $('input').blur(function(e){
    
            if( $(this).val() == '' ){
    
                $(this).next('.req').show();
    
            }else{
    
                $(this).next('.req').hide();
    
            }
    
        });
    })
    

    but it’f only work when i focus filed and leave it
    i want make it work evenif i submit also
    and i want use preventDefault() if it one required

    #151793
    amis
    Participant

    I have been fixed it
    this is my code

    $(function(){
    
    $('form').submit(function(e){
    
                $('input,textarea').each(function() {
                        if( $(this).val() == '') {
                                if( $(this).next('.req').length == 0 ){     
                                    $('<span class="req"></span>').insertAfter( $(this) );
                                    var data = $(this).attr('data-req');
                                    $(this).next('.req').text(data).show();
                                }
                            e.preventDefault();
                        }else{
                            $(this).next('.req').remove();
                        }
                });
    
    
    });
    
    })
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.