1

I have this HTML:

<div id="studyTestContent" class="studyTestItem">
    <input type="text" class="dropInput" size="15">
    <ul class="inputDrop" style="display:none;">
        <li class="dropDownItem">Si</li>
        <li class="dropDownItem">y</li>
        <li class="dropDownItem">con</li>
        <li class="dropDownItem">el</li>
    </ul>
</div>

and I have this jQuery:

$('.dropInput').click(function() {
    var offset = $(this).offset();
    var height = $(this).height();
    var width = $(this).width();
    var top = offset.top + height + "px";
    var right = offset.left + width + "px";

    $(this).next().show();

    $(this).next().css({
        'position': 'absolute',
        'right': right,
        'top': top
    });
});

With this function I am attempting to show the <ul> when the input is clicked. Nothing happens when I click it. Any ideas why?

Edit: I just figured out what the problem was, I am inserting the html after the page loads so I need to do:

$('.dropInput').live('click', function() {
    var offset = $(this).offset();
    var height = $(this).height();
    var width = $(this).width();
    var top = offset.top + height + "px";
    var right = offset.left + width + "px";

    $(this).next().show();

    $(this).next().css({
        'position': 'absolute',
        'right': right,
        'top': top
    });
});
7
  • 3
    It works on jsFiddle; there must be something wrong somewhere else in the page. Commented Jun 14, 2011 at 18:27
  • Is the click event not firing or is there an issue with what supposed to execute upon firing? Commented Jun 14, 2011 at 18:28
  • Are you executing that JavaScript in a document ready handler? Commented Jun 14, 2011 at 18:28
  • Really? It seems OK here: jsfiddle.net/3SF2Z Commented Jun 14, 2011 at 18:28
  • 1
    Yes, that would change things. The event is never bound to the new HTML. Use jQuery live() to bind the event to the new HTML. Commented Jun 14, 2011 at 18:32

4 Answers 4

3

Make sure you wait until the document is ready with

$(document).ready(function() {
    // put all your jQuery goodness in here.
    $('.dropInput').click(function() {
        var offset = $(this).offset();
        var height = $(this).height();
        var width = $(this).width();
        var top = offset.top + height + "px";
        var right = offset.left + width + "px";

        $(this).next().show();

        $(this).next().css({
            'position': 'absolute',
            'right': right,
            'top': top
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Since you are inserting the HTML into the document after the initial, you will need to use jQuery live() to bind the event to the new element.

1 Comment

For future reference, live() is now deprecated. Use on() instead: api.jquery.com/on
0
$(this).next(":hidden").show();

Comments

0

Try:

$(document).ready(function(){
    $('.dropInput').click(function() {
        var offset  =   $(this).offset(),
            height  =   $(this).height(),
            width   =   $(this).width(),
            top     =   offset.top + height + "px",
            right   =   offset.left + width + "px";

        $(this)
            .next('.inputDrop')
            .show()
            .css({
                'position': 'absolute',
                'right': right,
                'top': top
            });
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.