0

my code is only working once. When I click on the button, the code executes fine. But when I do the same after it executed fine once, it doesn't work, I don't even get the alert('clicked').

Anyone knows why? Here is the code:

$('#eucontentpage #country-choice .item .lang-ico').click(function(){
    alert('clicked');
    $(this).parent().removeClass('item');
    var country = $(this).parent().attr("class");
    var language = $(this).html();
    var pathArray = window.location.pathname.split( '/' );
    var newPathname = "";
    for (i = 0; i < 5; i++) {
      newPathname += pathArray[i];
      newPathname += "/";
    }
    var loadlink = newPathname + country + "/index_"+ language +".htm #nationalcontentpage";

    $.when(
        $('.lightbox').after('<div id="insertnationalcontent"></div>')
    ).done(function() {
        $("#insertnationalcontent").load(loadlink),
        $('#jb-window').hide(),
        $('#jb-window-content').hide(),
        $('#jb-overlay').hide(),
        $(this).parent().addClass('item'),
        $('#insertnationalcontent').fadeIn('slow'),
        $.history.load( '' )
    });

    return false;
});
5
  • I'm guessing this line is causing the jQuery selector not to match anymore: $(this).parent().removeClass('item'); Commented Feb 25, 2015 at 13:07
  • I tried to leave it out of the click event, but it doesn't change a thing: $('#eucontentpage #country-choice .lang-ico').click(function(){ Commented Feb 25, 2015 at 13:14
  • Please create a jsfiddle Commented Feb 25, 2015 at 13:20
  • @jens_vdp, can you also add your HTML in the question or better yet, create a JSfiddle :) Commented Feb 25, 2015 at 13:24
  • do $('#eucontentpage #country-choice).find('.lang-ico').click(...); Commented Feb 25, 2015 at 14:10

3 Answers 3

1

Try it like this:

$('#eucontentpage #country-choice').on("click",".item .lang-ico",function(){ // this first part is different
    alert('clicked');
    $(this).parent().removeClass('item');
    var country = $(this).parent().attr("class");
    var language = $(this).html();
    var pathArray = window.location.pathname.split( '/' );
    var newPathname = "";
    for (i = 0; i < 5; i++) {
      newPathname += pathArray[i];
      newPathname += "/";
    }
    var loadlink = newPathname + country + "/index_"+ language +".htm #nationalcontentpage";

    $.when(
        $('.lightbox').after('<div id="insertnationalcontent"></div>')
    ).done(function() {
        $("#insertnationalcontent").load(loadlink),
        $('#jb-window').hide(),
        $('#jb-window-content').hide(),
        $('#jb-overlay').hide(),
        $(this).parent().addClass('item'),
        $('#insertnationalcontent').fadeIn('slow'),
        $.history.load( '' )
    });

    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

0

You are removing the "item" class from the parent object. while using it in the object reference "#eucontentpage #country-choice .item .lang-ico".

After removing the item class your referens does not match the any more.

try using just

$('.lang-ico').click(function(){...}

Comments

0

You are doing $(this).parent().removeClass('item'); So when this code executes it removes item class that's why your DOM selector is not matching.

Just comment below line your code will be executed multi time. $(this).parent().removeClass('item');

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.