How to make my function only affect the element that is being clicked

1

I have this function, but when it is executed it is applied to all the elements and since I am using its css style reference to grasp the elements, all are painted and not only the one that is clicked

 $('.vermas').click(function(){

    $('.elemento-preguntas').addClass( "elemento-activado" );
    $('.respuesta-icon').text( "!" );
    $('.respuesta').css( 'display','block');
    $('.vermas').css( 'display','none');

  })
    
asked by Luis Morales Ponce 28.03.2018 в 02:29
source

3 answers

2

It applies to everyone because you are directly accessing your class which is generic I guess since you can not see all your code; if you want it to only apply to a specific element use the id that is assigned

$('#id_unico').click(function(){
//aquí debería ir el código que quieres afectar de los demás elementos pero todos por medio de su id que ayudan a identificar un elemento de manera única
})

Remember that to access a class is .class and to access it by means of its id is #id

// UPDATE

 $('#boton_id').click(function(){

    $('#id_uno').addClass( "elemento-activado" );
    $('#id_dos').text( "!" );
    $('#id_tres').css( 'display','block');
    $('#boton_id').css( 'display','none');

  })
    
answered by 28.03.2018 в 02:33
2

Doing it like that worked for me

$('.elemento-preguntas').click(function(){

    $(this).addClass('elemento-activado')
    $(this).find('span').text( "!" );
    $(this).find('button').css( 'display','none');
    $(this).find('p').css( 'display','block');


  })
    
answered by 28.03.2018 в 03:01
0

Use the this property as the jQuery selector of the element

 $('.vermas').click(function(){
    $(this).addClass( "elemento-activado" );
    })
    
answered by 28.03.2018 в 02:42