Assign dynamic value to a variable in Jquery

1

I searched a lot and could not find the answer.

I need to assign a value to a variable in Jquery, I do not have much knowledge. What I need is:

When you click on a link:

<a href="?sts=Pendente" title="Avisos Pendentes" id="Pendente" data-statusTipo="Pendente">

A variable named sts1 has the id value above:

var sts1 = $('a').on(click (function() {
  $('a:hover', this).data('statusTipo');
}));

That's it ... I tried everything and I could not ...

Thank you for your attention.

    
asked by anonymous 13.04.2016 / 01:38

3 answers

2

When you use $('a').on(click (function() { jQuery calls this function when the element receives the event. Within this function this receives an indicator for the element. So, within this function you can use:

var id = this.id; // para saber o id
var status = this.dataset.statusTipo; // para aceder a 'data-'

So you can use:

$('a').on(click (function() {
    var id = this.id; // para saber o id
    var status = this.dataset.statusTipo; // para aceder a 'data-'
    alert(id + ' ' + status);
}));
    
13.04.2016 / 12:47
0

Try this:

$(document).ready(function() {
 var sts1 = '';

 $('a').click(function() {
   sts1 = $(this).data('statusTipo');
 });
});
    
13.04.2016 / 01:47
0

Friend, Jquery has a function called attr() . The syntax of the command is:

$( seletor ).attr( 'nome_atributo' );

Now to solve your problem, please do:

$(document).ready(function() {
     var sts1 = '';

     $( 'a' ).click(function() {
         sts1 = $(this).attr( 'data-statusTipo' );
     });

});

I hope that helps you.

    
13.04.2016 / 02:43