2

I have this javascript :

<script type="text/javascript">
$(document).ready(function($) {
        $('#target-share ul li').click(function() {
            $('input#shareto').val($(this).data('val'));
        });
    });
</script>

and it really works as I expected. but when I add this another javascript to do drop-down animation, that javascript not working anymore :

<script type="text/javascript">
$(document).ready(function() {
    $("#select-shareto a").click(function () {
        var newClass = $(this).find("span").attr('class');
        var newText = $(this).find("span").text();
        $("#select-shareto a").removeClass("selected");
        $(this).addClass("selected");
        $("a#to-chosen span").removeClass().addClass(newClass).text(newText);
        $("a#to-chosen").toggleClass("opened");
        $('#select-shareto').slideToggle('fast');
        return false;
    });

    $('a#to-chosen').click(function() {
        $(this).toggleClass("opened");
        $('#select-shareto').slideToggle('fast', function() {
            // Animation complete.
        });
    });
});
</script>

those javascript actually affected on this HTML code :

<div id="target-share">
<a href="#" title="" id="to-chosen" class="default"><span class="to-admin">Admin</span></a>
<ul id="select-shareto">
<li data-val="0-1">
<a href="#" title="" class="selected"><span class="to-Finance">Finance</span></a>
</li>
<li data-val="1-1">
<a href="#" title=""><span class="to-admin-private">Admin Private</span></a>
</li>
<li data-val="1-0">
<a href="#" title=""><span class="to-ceo">CEO</span></a>
</li>
<li data-val="0-0">
<a href="#" title=""><span class="to-ceo-private">CEO Private</span></a>
</li>                                    
</ul>
<input id="shareto" type="text" value="0-1" name="shareto">
</div><!-- #target-share -->

any idea how to make those 2 javascript works side-by-side? thanks.

7
  • They work alone and they prefer to be by themselves. Commented Jul 27, 2012 at 18:23
  • 1
    Is there anything in the Javascript console? Maybe you misplaced a brace somewhere Commented Jul 27, 2012 at 18:23
  • Why you are passing the "$" as paramter in the first function to the "ready" event? Commented Jul 27, 2012 at 18:28
  • 1
    Ok, seriously now. Have you tried putting it into a single ready function block? Commented Jul 27, 2012 at 18:31
  • 1
    @kakridge: That doesn't matter. @Estefano: That's a common technique to ensure that $ really points to jQuery, especially when used with noConflicts() Commented Jul 27, 2012 at 18:42

2 Answers 2

7
$("#select-shareto a").click(function () {
    ...
    return false;
});

Hereby, you stop the further propagation of this event - other handlers for this click event won't be called any more. Instead, use this:

$("#select-shareto a").click(function (e) {
    e.preventDefault();
    ...
});

(Demo at jsfiddle.net)

Sign up to request clarification or add additional context in comments.

1 Comment

IT WORKS!!! thanks bro!!! I must wait another 3 minutes to toggle your answer... great job, bro!! thanks again...
0

I'm assuming you tried to insert the code where the comment animation complete is and if so you just need to put the code:$('#target-share ul li').click(function() { $('input#shareto').val($(this).data('val'));});

Your code is already wrapped in a $(document).ready so you don't need another one.

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.