skip to Main Content

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!

2

Answers


  1. This is for single text change. Not for array.

    $(document).ready(function(){
      $(".txtchange").click(function(){
        $("p").text("New Text!");
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    
    <button class="txtchange">Change</button>
    
    <p>A Simple Text.</p>
    Login or Signup to reply.
  2. Try this:

    let quots = [
      'quot1',
      'quot2',
      //...
    ];
    
    $(document).on('click', '#button', function(){
        $('#quotBox').text(quots[Math.floor(Math.random() * quots.length)]);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search