gidzior
gidzior

Reputation: 1805

Changing attr on click working only once

Hi my brothers developers, i need help with changing attr in jquery, i have list of apartments with some info and photos and i would like to change those information after click on apartment name, i wrote a function that works perfectly but only once, nothing happens on second click, is there something i missing ??

HTML

<!-- apartments list -->

<li id="lokal_01">rzut</li>
<li id="lokal_02">rzut</li>
<li id="lokal_03">rzut</li>
<li id="lokal_04">rzut</li>

<!-- container of informations -->

    <div id="oferta_lokal_" class="szczegoly">
        <div class="close" title="zamknij"></div>
        <div class="pdf"><a href="" onclick="this.target='_blank'"></a></div>
        <div class="potoki-logo">potoki Residence mokotów</div>
        <div class="liquid-slider"  id="slider_lokal_">
            <div><h2 class="title">Rzut</h2><img class="rzut" src=""></div>
            <div><h2 class="title">Lokalizacja</h2><img class="lokalizacja" src=""></div>
        </div>
    </div>

JQUERY

$.each(['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'], function (index, value) {
        $('#lokal_' + value).on('click', function(){

            $('#oferta_lokal_').attr('id','oferta_lokal_'+value);
            $('#slider_lokal_-wrapper').attr('id','slider_lokal_'+value+'-wrapper');
            $('.pdf a').attr('href','img/rzuty/lokal_'+value+'.pdf');
            $('#slider_lokal_').attr('id','slider_lokal_'+value);
            $('img.rzut').attr('src','img/rzuty/lokal_'+value+'_rzut.jpg');
            $('img.lokalizacja').attr('src','img/rzuty/lokal_'+value+'_lokalizacja.jpg');

            $('#oferta_lokal_' + value).show();
        });
    });

Upvotes: 0

Views: 624

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

Because you change the id attributes so you can no longer select with them:

$("#oferta_lokal_").attr("id", "oferta_lokal_" + value)

At this time, #oferta_lokal_ does not exist, so it won't be found when you click it again. I'm not sure why you need to change the ID, but I would avoid doing so. Instead, add/toggle a class or use a dataset attribute (.data("id", value))

Upvotes: 2

Related Questions