문제

I want to add class "unblock" to all elements contanining desbloquear var value (in this case, taken from clicked a element's id).

I am not able to do this... as there are several ements that should receive the new class, I can not set the same id to them (to be equal as "a" id). How could I say, on the last line of the function, that all items containing "a" class (desbloquear var value) should add class unblock?

I've tried

$(.desbloquear).addClass('unblock');

and

$(".desbloquear").addClass('unblock');

without result...

$('a').on('click', unblock);

function unblock(){

    var desbloquear = $(this).attr('id');

    $(desbloquear).addClass('unblock');

}
도움이 되었습니까?

해결책

all items containing "a" class (desbloquear var value) should add class unblock

I might not got your question correctly but by above line. It looks that you want to add a class unblock to elements those have some specific class. if yes then you can do like this

function unblock(){

    var desbloquear = $(this).attr('class');

    $('.'+ desbloquear).addClass('unblock');
}

다른 팁

Yes you are correct, Id's should be unique, you can use class instead

You will need a . to specify a class attribute in selector

$('.'+desbloquear).addClass('unblock');

class selector should begins with a .

Just try,

$('.' + desbloquear).addClass('unblock');

Full code:

$('a').on('click', unblock);

function unblock() {
    var desbloquear = $(this).attr('id');
    $('.' + desbloquear).addClass('unblock');    
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top