1

I have this working jQuery + inline javascript which causes a conflict with existing jQuery.

<script>
var jq=jQuery.noConflict(); 
function goto(id, t){   
    jq(".contentbox-wrapper").animate({"left": -(jq(id).position().left)}, 600);
    jq('#slide a').removeClass('active');
    jq(t).addClass('active');   
}
</script>
<a class="active" href="#" onClick="goto('#kr', this); return false">
<a class="active" href="#" onClick="goto('#en', this); return false">

(I've tried to resolve the conflict as you can see but I believe the conflict arises from the inline javascript.)

How can I convert this inline javascript? Thanks.

1
  • 1
    what do you mean when u say it has conflict with the existing jquery? Commented Mar 26, 2013 at 18:44

3 Answers 3

1

You can bind it like:

<script>
//var jq=jQuery.noConflict();   
function goto1(id, t){   
    ...
    return false; // return false to prevent the link's default action
}

// means once your DOM is ready, and a.active is available to be bound
$(document).ready(function() { 

    // bind all clicks on a.active to the function 'goto1'
    $('a.active').click(goto1);
});
</script>

Variable names like goto can be potential causes of confusion later on. Changed it above to goto1.

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

Comments

0

Inline JS (embed into HTML) is hardly maintainable, I'd suggest:

HTML:

<div id="parent"> <!-- or any other parent el. -->
    <a href="#">Anchor</a>
</div>

jQuery:

(function($){ // remap $ to jQuery

  $(function(){ // DOM ready

    $('#parent').on('click', 'a', function( ev ){
       ev.preventDefault();
       var krPos = $('#kr').position().left;
       $(".contentbox-wrapper").animate({"left": -krPos }, 600);
       $('#slide a').removeClass('active');
       $(this).addClass('active'); 
    });

  });

})(jQuery);

3 Comments

You can remove the outer function, and just do jQuery(function($) { // DOM ready. The jQuery function will be passed to the $ parameter of the .ready() callback, giving you the scope protection you want.
@amnotiam But I'll leave it like it is cause you can put other things like $(window).load(function(){ and others - always inside the first function (remapper) perimeter.
I am clueless as the above code does not animate/move to #kr & #en.
0
$('.active').on('click', function() {
var $this = $(this);        
var id = $this.attr('href');
$(".contentbox-wrapper").animate({"left": -($(id).position().left)}, 600);
$('#slide a').removeClass('active');
$this.addClass('active');
return false;
 });

huangism answered it for me. https://stackoverflow.com/users/1058134/huangism

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.