简体   繁体   中英

Jquery - Get attribute value of closest class

I am trying to grab the attribute value from the closest element with the class specified but keep getting back undefined.

Below is my HTML and my code, I am not too sure what am doing wrong to get back undefined.

HTML

    <body>
  <form action="/update" id="formwrite">
    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
      <div class="panel">
        <div class="panel-heading" role="tab" id="headingOne">
          <h4 class="panel-title va-middle">Account Settings
                        <img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeOne" aria-expanded="true" aria-controls="collaspeOne">
                        </h4>
        </div>
        <div id="collaspeOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-default">
            <div class="row fieldpos">
              <fieldset class="form-group textwide">
                <label for="email">Email</label>
                <input type="email" class="form-control" id="email" placeholder="Enter Email" required>
              </fieldset>
            </div>
          </div>
        </div>
      </div>
      <div class="panel">
        <div class="panel-heading" role="tab" id="headingTwo">
          <h4 class="panel-title va-middle">Other Settings
                                <img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeTwo" aria-expanded="true" aria-controls="collaspeTwo">
                                </h4>
        </div>
        <div id="collaspeTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-default">
            <div class="row fieldpos">
              <fieldset class="form-group textwide">
                <label for="group">Test</label>
                <input type="text" class="form-control " id="group" placeholder="test" required>
              </fieldset>
            </div>
          </div>
        </div>
      </div>
    </div>
    <button type="submit" class="btn btn-success form-control savechanges">Save Changes</button>
    <button class="btn btn-success form-control test">Call Function</button>
  </form>
</body>

JS

  $(document).on("click", ".test", function (){
    $("input[required]").each(function(){
    var inputval = $(this).val();
    var collaspe = $(this).closest(".collapse-icon").attr("aria-expanded");
    console.log(collaspe);
        if(!inputval){
            console.log("Required Empty");
            $(".collapse-icon").trigger("click");
        } else {
            console.log("Required has value");;
        }
    });
    });

Any suggestions?

EDIT: JSFiddle

$.closest traverses up the DOM searching only ancestors, it will not search for the "literal" closest element. Your .collapse-icon don't appear to be direct parents, but rather are children of the .panel-heading elements (which are parents). So the solution would be $(this).closest('.panel-heading').find('.collapse-icon')

UPDATE : I apologize, in my answer I mispoke. It should be .closest('.panel').find('.panel-heading .collapse-icon') since .panel-heading is a sibling of the or input's parent. So you need the .panel-body 's parent and then find it's heading collapse icon.

As already explained closest only search in direct ancestors.

Here is a working fiddle - https://jsfiddle.net/z5wbxzzo/2/

  $(document).on("click", ".test", function (){
    $("input[required]").each(function(){
    var inputval = $(this).val();
    var collaspe = $(this).closest(".panel-collapse").siblings(".panel-heading").find(".collapse-icon").attr("aria-expanded");
    console.log(inputval, collaspe);
        if(!inputval){
            console.log("Required Empty");
            $(".collapse-icon").trigger("click");
        } else {
            console.log("Required has value");;
        }
    });
    });

hope this helps

closest() return the first ancestor matching the specified selector.

.collapse-icon is not an ancestor of input[required]

As I guess there is more than one .panel element in a .panel-group you will have to identify first the top .panel element (by an id or a specific class name or whatever) next dive into it to find the .collapse-icon

html code

<div class="panel panel-container">
    <div class="panel-heading" role="tab" id="headingOne">
        <h4 class="panel-title va-middle">Account Settings
            <img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeOne" aria-expanded="true" aria-controls="collaspeOne">
        </h4>
    </div>

    ...

js code

$(this).closest('.panel-container').find('.collapse-icon')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM