Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to .parent().parent().siblings() in jQuery

Tags:

jquery

I've been having a hard time getting the right selector and I've just now got it working by doing .parent().parent().siblings() , but I know there has to be a better way to get it without chaining it so much.

The div is added dynamically and this is the only reliable way I've found to select it:

var current = $(".filename:contains('" + file.name + "')").parent().parent().siblings();
current.find("input[name=title]").val(obj.file_name);

This is the jsFiddle link: http://jsfiddle.net/Msnf9/8/

This is the HTML:

<div id="uploadifive-fileupload-queue" class="uploadifive-queue">
    <div class="uploadifive-queue-item" id="uploadifive-fileupload-file-0">
        <div class="span12 well">
            <div class="row-fluid">
                <div class="alert">
                    <div class="filename">file-name-1.jpg</div>
                    <div class="fileinfo"> - Completed</div>
                </div>
                <div class="progress">
                    <div class="bar"></div>
                </div>
            </div>

            <div class="row-fluid inputs">
                <div class="span3">
                <ul class="thumbnails">
                    <li class="span12">
                      <a href="#" class="thumbnail">
                        <img src="http://placehold.it/260x180" alt="">
                      </a>
                    </li>
                  </ul>
                </div>
                <div class="span9">
                    <form class="form-horizontal">
                        <fieldset>
                            <div class="control-group">
                                <label class="control-label" for="file-name">File Name</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-name" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="file-dimensions">File Dimensions</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-dimensions" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="file-url">File URL</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-url" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="alt-text">Alt Text</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" placeholder="Alt text" name="alt-text" id="alt-text" />
                                </div>
                            </div>
                            <input type="hidden" name="image_id" />
                            <div class="form-actions">
                                <button id="save" type="button" class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>
                                <button id="delete" type="button" class="btn btn-inverse"><i class="icon-trash icon-white"></i> Delete</button>
                         </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
like image 349
Motive Avatar asked Feb 20 '23 21:02

Motive


1 Answers

You can use closest() method:

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

var current = $(".filename:contains('" + file.name + "')").closest('.row-fluid').siblings();
like image 143
undefined Avatar answered Mar 02 '23 23:03

undefined