3
I’m developing a script that requires calling a click()
from a button as it is invisible on the page, however jQuery does not seem to return the updated attributes when they are changed. Take this example:
$(function(){
$('.hidden').on('click', function(){
console.log($(this).data('s'));
alert($(this).data('s'));
});
});
$('.visivel').on('click', function(){
var d = new Date();
var s = d.getSeconds();
$('.hidden').attr('data-s', s).click();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class='visivel'>Clique me</button>
<button class='hidden' data-s='' style='display:none;'>Invisivel</button>
As you can see, testing generates a alert()
informing the current seconds, but if you click again it keeps showing the seconds of the first click, despite the data-s
be amended, the alert
does not return the new second. Could this be a jQuery bug? I could only solve using this.dataset.s
so that the alert
work, would you like to know why this behavior? And the structure is the same, a function inside and outside the $(function(){})
.
PS:
The structure must follow what I have set as an example, one of the functions must be out of $(Function(){}), because the logic of project requires this type of organization, I am using other libraries.
Now yes I understand, actually the problem is how jQuery works when dealing with attributes, so I will only use
data()
in both.– Paulo