Search code examples
htmljqueryreactjscodepen

Using jQuery - By clicking a button change the text of a div


I am working on a project in CodePen editor. My problem is: How to change the text of a element each time a hit the button ?

I have an array from which I am displaying random text on a div. Thanks!


Solution

  • Try this:

    let quots = [
      'quot1',
      'quot2',
      //...
    ];
    
    $(document).on('click', '#button', function(){
        $('#quotBox').text(quots[Math.floor(Math.random() * quots.length)]);
    });