Qcom
Qcom

Reputation: 19213

Checking if a DOM element is draggable and setting equal to JavaScript variable?

What is the most efficient way to create a JavaScript variable equal to the "draggable" state of a DOM element via jQuery?

I tried the following:

$(document).ready(function() {
    $('#editButton').click(function() {
        var isDraggable = $('.tile').draggable('option', 'disabled');
        if(isDraggable == "true")
        {
            $(function() {
                $('.tile').draggable({
                    containment: '#content',
                    scroll: false
                });
                $('.tile').resizable({
                    maxHeight: 200,
                    maxWidth: 200,
                    minHeight: 100,
                    minWidth: 100
                });
            })
        }
    })
});

I didn't get any syntax errors or anything, but the functionality wasn't there.

Here is the accompanying HTML:

<div id="content">
        <div id="navBar">
            <input type="button" id="editButton" value="Edit" />
        </div>
        <ul>
            <li>
                <div id ="google" class="tile">
                    <img src="images/tiles/google/google.png" class="tile_image" width="27" height="41" />
                    <p>Google</p>
                </div>
            </li>
            <li>
                <div id="yahoo" class="tile">
                    <p>Yahoo</p>
                </div>
            </li>
            <li>
                <div id="thelockpeople" class="tile">
                    <p>The Lock People</p>
                </div>
            </li>
            <li>
                <div id="amazon" class="tile">
                    <p>Amazon</p>
                </div>
            </li>
            <li>
                <div id="masterlock" class="tile">
                    <p>Masterlock</p>
                </div>
            </li>
            <li>
                <div id="hpionline" class="tile">
                    <p>Hpionline</p>
                </div>
            </li>
            <li>
                <div id="youtube" class="tile">
                    <p>YouTube</p>
                </div>
            </li>
            <li>
                <div id="kitco" class="tile">
                    <p>Kitco</p>
                </div>
            </li>
            <li>
                <div id="usatoday" class="tile">
                    <p>USA Today</p>
                </div>
            </li>
            <li>
                <div id="gamefly" class="tile">
                    <p>GameFly</p>
                </div>
            </li>
        </ul>
</div>

What am I doing wrong?

Upvotes: 4

Views: 2166

Answers (1)

Neil
Neil

Reputation: 3231

To make it work, just replace the line that says:

if(isDraggable == "true")

with:

if (isDraggable)

The reason is that isDraggable is an object, if it exists (i.e. the "draggable" method on the ".title" div worked) then it will be true (exists and is non-zero), but it will not be equal to 1, which is the numeric value for true, so your original test fails and you never call the function.

Upvotes: 4

Related Questions