How to force a button click using javascript

How to force a button click using javascript

  

Hi. Good day devs. :) Please help me. I have a 'comments' page(popup) on my project and i need to force a button click using a javascript to refresh the data and widget(list of comments). 

SyntaxEditor Code Snippet

"var timeout = setInterval(reloadChat, 3000);
function reloadChat () {
   document.getElementById('+refreshcombtn.Id+').trigger('click');
}"

(The javascript snippet that is on the Preparation of the page) (img 1.2 is the 'comments page' the refreshcomments button is hidden). The 'refreshcombtn.Id is the id of the button that has a data refresh and widget refresh. it is set to be refreshed every 3 seconds. but it doesn't. Thank you for your replies. :)

Hi Jan,

I'd say in the Preparation no widget Ids will have been generated yet... this code would then need to be executed while rendering the screen. I'd suggest a JS (or jQuery) pattern (if you're using Silk UI), or place your JavaScript code in an Expression with Escape Content set to No.

Jorge Martins wrote:

Hi Jan,

I'd say in the Preparation no widget Ids will have been generated yet... this code would then need to be executed while rendering the screen. I'd suggest a JS (or jQuery) pattern (if you're using Silk UI), or place your JavaScript code in an Expression with Escape Content set to No.

Hi sir Jorge. Thanks for the reply. i transferred the code to the javascript of the page.  and i 

highlighted the widget refresh.There's no error but nothing happens.


As Jorge mentioned, you'd need to put your javascript logic in an expression, not the page's javascript property.

Encapsulate your expression like this:

"<script>

$(document).ready(function(){

    your logic...

});

</script>"


João Melo wrote:

As Jorge mentioned, you'd need to put your javascript logic in an expression, not the page's javascript property.

Encapsulate your expression like this:

"<script>

$(document).ready(function(){

    your logic...

});

</script>"


Good day sir. i have put this snippet on to an expression but it doesn't work.

SyntaxEditor Code Snippet

"<script>
var timeout = setInterval(reloadChat, 3000);
function reloadChat () {
   jQuery('refreshcommentbtn.Id').click();
  
}
</script>"

is there anything wrong? Thanks

You missed the "$(document).ready(function(){"

João Melo wrote:

You missed the "$(document).ready(function(){"

I've augmented this snippet but still nothing happens.


Hello Jan Carlo Duterte,

yes João Melo sir say right and please refer below code as your requirement....

"<script type="text/javascript">

$(document).ready(function(){

 var timeout = setInterval(reloadChat, 3000);

function reloadChat () {

 jQuery('refreshcommentbtn.Id').click();

}});

</script>"


Jan,

you will need to do some string concatenation there...

"<script>
$(document).ready(function(){
  var timeout = setInterval(reloadChat, 3000);
  function reloadChat () {
    $('" + refreshcommentbtn.Id + "').click();
  
  }
})
</script>"

Off course... the quick answer always misses something... ;) Good point Jorge..


Jorge Martins wrote:

Jan,

you will need to do some string concatenation there...

"<script>
$(document).ready(function(){
  var timeout = setInterval(reloadChat, 3000);
  function reloadChat () {
    $('" + refreshcommentbtn.Id + "').click();
  
  }
})
</script>"



Amit Verma wrote:

Hello Jan Carlo Duterte,

yes João Melo sir say right and please refer below code as your requirement....

"<script type="text/javascript">

$(document).ready(function(){

 var timeout = setInterval(reloadChat, 3000);

function reloadChat () {

 jQuery('refreshcommentbtn.Id').click();

}});

</script>"


yeah i've done this


Jorge Martins wrote:

Jan,

you will need to do some string concatenation there...

"<script>
$(document).ready(function(){
  var timeout = setInterval(reloadChat, 3000);
  function reloadChat () {
    $('" + refreshcommentbtn.Id + "').click();
  
  }
})
</script>"


Thanks for this. but still nothing happens. or is it because it is a pop up page? Thanks

Solution
Actually, there was a couple more missing...
"<script>
$(document).ready(function(){
  var timeout = setInterval(reloadChat, 3000);
  function reloadChat () {
    $('#" + refreshcommentbtn.Id + "').click();
  
  }
});
</script>"

If it doesn't work, send us your oml

Solution

There is a missing # in your script, it should be like this:

$('#" + refreshcommentbtn.Id + "').click();

and I am too slow :)


Thank you so much Master Devs for your immediate replies. :) i think its already done :) Godspeed

mind you, it *might* be better to use requestAnimationFrame instead of setinterval for better consistency (and it wil stop when the tab becomes inactive)


J. wrote:

mind you, it *might* be better to use requestAnimationFrame instead of setinterval for better consistency (and it wil stop when the tab becomes inactive)


ooh. i'll check on that. Thank you sir.