2
The elements are generated dynamically, thanks to this it does not exist in the DOM. My case is the following:
<div class="ui-grid-a">';
<div class="ui-block-a">'+row.nome_item+' id '+row.id_item_conta+'<div class="valor_quantidade" quantidade_item="'+quantidade+'">'+quantidade+' X '+row.valor_item+'</div></div>
<div class="ui-blok-b"><a href="javascript:0" id_item_conta="'+row.id_item_conta+'" class="ui-btn ui-icon-plus ui-btn-icon-right soma_item_conta" style="background: none; border: none;float:right;margin-top:5px;"></a><a href="javascript:0" id_item_conta="'+row.id_item_conta+'" class="ui-btn ui-icon-minus ui-btn-icon-right diminui_item_conta" style="background: none; border: none;float:right;margin-top:5px;"></a></div>
</div>
These elements are generated dynamically and placed inside a div
with certain id
:
<div id="conta_selecionada"></div>
So far so good, notice that there is a link with class soma_item_conta
to recognize this link is easy because there is an event attached to it.
$('#conta_selecionada').on('click','.soma_item_conta',function(){
//alert('TETSE');
});
But then my problem begins. I need to recover the value of the attribute quantidade_item
that was generated dynamically along with the link a
... However as he has no event attached to it I can’t recover along with the on
.
Does anyone have any solution?
If you use the class instead of the ID to select the component, the events associated with it will also be attached to the new controls created with the same class. Ex:
$('.classe_deste_e_dos_novos_componentes').on('click'...
.– Caffé
I didn’t understand @Caffé could explain better?
– Luk Simões
It was I who did not understand your question. If the new elements are created within the div
#conta_selecionada
and whether these new elements receive thesoma_item_conta
, then the way you associate code to the event click these new elements ($('#conta_selecionada').on('click','.soma_item_conta',function(){...
) is correct. Each new element, when clicked, must trigger its code because the event was associated with a previously existing element (#conta_selecionada
) and delegated to their class child elementssoma_item_conta'
. So that I could not understand his problem.– Caffé
Friend Thank you next the elements are created dynamically the quantity_item attribute is not son of soma_item_account but brother. and it has no event associated with it just need to get the value of attr.
– Luk Simões
Edit the question to make it clearer. Consider showing more relevant html and Javascript. Consider also showing the already solved html instead of the string concatenations. I have the impression that your doubt can be very easily resolved here in the OS as long as it is properly formulated.
– Caffé