Preston
Preston

Reputation: 2185

Add class to a element from parent page

Ok.. i have some parent page, let's call it test.html

Inside of that page, i'm loading other 2 external pages: test1.html and test2.html.

With this script:

$("#linhas_cat li a").click(function(e) {
            e.preventDefault();
            var current_linha = $(this).attr("class");
            var current_link = $(this).attr("href");
            var alternate_link = $(this).attr("data-link");

            $('#linhas_cat li a').removeClass('active');

            $("#wrap_linhas_fake ul li").css("background", "#b0aa9d");

            $(this).addClass('active');

            $("#wrap_linhas_fake ul li").filter("." + current_linha).css("background", "#ed1d24");

            $("#container_produto").load(current_link).hide().fadeIn('slow'); //external page test1.html
            $(".conteudo_catalogo").load(alternate_link).hide().fadeIn('slow');//external page test2.html

        });

Inside of page teste2.html i have some menu. Like This:

<ul class="nome_linhas">
    <li><a href="exibe_linhas.html?sublinha=1">Nome da Linha</a></li>
    <li><a href="exibe_linhas.html?sublinha=2">Nome da Linha</a></li>
    <li><a href="exibe_linhas.html?sublinha=3">Nome da Linha</a></li>
    <li><a href="exibe_linhas.html?sublinha=4">Nome da Linha</a></li>
</ul>

I need to apply a class when the link is clicked. Class active, but same time, I have to open your link (exibe_linhas.html?sublinha=1) in the sameplace that is currently, inside the div .conteudo_catalogo

I have tried that way, on the "mother" page:

<script type="text/javascript">
  jQuery(document).ready(function($){
    $(".conteudo_catalogo .nome_linhas li a").live('click', function(e) {
        e.preventDefault();
        var teste_link = $(this).attr("href");
        $(".conteudo_catalogo .nome_linhas li a").removeClass('active');
        $(".conteudo_catalogo").load(teste_link).hide().fadeIn('slow');
        $(this).addClass('active');
    });
  });
</script>

But, when the page is loaded, the class was not added...

How to handle with this?

Upvotes: 0

Views: 80

Answers (1)

adeneo
adeneo

Reputation: 318182

$(".conteudo_catalogo").on('click', '.nome_linhas li a', function(e) {
    e.preventDefault();

    var idx = $(this).closest('li').index();

    $(".conteudo_catalogo").hide().load(this.href, function() {
        $(".nome_linhas li:eq("+idx+")", this).addClass('active')
        $(this).fadeIn('slow');
    });
});

Upvotes: 2

Related Questions