I am developing a script that requires calling a click()
of a button because it is invisible on the page, however jQuery does not seem to return the updated attributes when they are changed. Here's an 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><buttonclass='visivel'>Cliqueme</button><buttonclass='hidden'data-s=''style='display:none;'>Invisivel</button>
Asyoucansee,whenyoutest,aalert()
isgeneratedinformingthecurrentseconds,butifyouclickagainitshowsthesecondsofthefirstclick,eventhoughdata-s
ischanged,alert
doesnotreturnthenewsecond.IsthisajQuerybug?Ionlymanagedtosolveusingthis.dataset.s
sothatalert
works,wouldyouliketoknowwhythisbehavior?Andthestructureisthesame,afunctioninsideandoutsidethe$(function(){})
.
link
PS:
The structure should follow the one I put as an example, one of the functions must be outside $ (function () {}), this is because the logic of project requires this type of organization, I am using other libraries.