skip to Main Content

I am trying to hide parent elements using ID of child as selector; if they do not contain specific text. In this case *. Must be this way and have to use jQuery.

So far I have this:

HTML

<div>
    <label>
        First name <font color="red">*</font>
        <input type="text" id="guest_first_name">
    </label>
</div>

</br>

<div>
    <label>
        Last name 
        <input type="text" id="guest_last_name">
    </label>
</div>

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<script>
 $(document).ready(function() {

    let checkthis = $("#guest_first_name, #guest_last_name").parent();  
    var removewithout = "*";
    checkthis.not(removewithout).hide();

 })

</script>

Struggling to check elements that do not contain text and then hide. Trying to hide <label> with text Last name as it does not contain *. Hope this is clear. Any help is appreciated.

Thanks

2

Answers


  1. The .not() method accepts selectors, jQuery objects, and functions.

    $(document).ready(function() {
    
      let checkthis = $("#guest_first_name, #guest_last_name").parent();
      var removewithout = /*/;
      checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
    
    })

    Reference:

    Login or Signup to reply.
  2. The .not() method in jquery & .not selector in javascript only accepts the objects, selectors and functions as parameters.
    But you are passing a character for that:

    Here is the more brief code that can help you:

    HTML:

      <div>
         <label>
            First name <font color="red">*</font>
            <input type="text" id="guest_first_name">
        </label>
      </div>
    
     </br>
    
     <div>
        <label>
            Last name 
            <input type="text" id="guest_last_name">
        </label>
     </div>
    

    JQUERY:

    $(document).ready(function() {
        let checkthis = $("input['text']").parent();
        var removewithout = /*/;
        checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
    });
    <div>
        <label>
            First name <font color="red">*</font>
            <input type="text" id="guest_first_name">
        </label>
    </div>
    
    <br>
    
    <div>
        <label>
            Last name 
            <input type="text" id="guest_last_name">
        </label>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search