15193
Views
16
Comments
Solved
How to force a button click using javascript
Question

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. :)

1.2.PNG
2023-02-10 19-42-59
João Melo
 
MVP
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

2019-09-24 18-41-25
Jorge Martins
 
MVP

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.

UserImage.jpg
Jan Carlo Duterte

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.


2023-02-10 19-42-59
João Melo
 
MVP

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>"


UserImage.jpg
Jan Carlo Duterte

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

2023-02-10 19-42-59
João Melo
 
MVP

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

UserImage.jpg
Jan Carlo Duterte

João Melo wrote:

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

I've augmented this snippet but still nothing happens.


2024-02-16 07-43-18
Amit Verma

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>"


UserImage.jpg
Jan Carlo Duterte

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


2019-09-24 18-41-25
Jorge Martins
 
MVP

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>"
2023-02-10 19-42-59
João Melo
 
MVP

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>"



UserImage.jpg
Jan Carlo Duterte

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

2023-02-10 19-42-59
João Melo
 
MVP
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

2016-04-21 20-09-55
J.
 
MVP

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

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

and I am too slow :)


UserImage.jpg
Jan Carlo Duterte

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

2016-04-21 20-09-55
J.
 
MVP

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


UserImage.jpg
Jan Carlo Duterte

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.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.