Home > Back-end >  Using jQuery - By clicking a button change the text of a div
Using jQuery - By clicking a button change the text of a div

Time:01-21

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!

CodePudding user response:

Try this:

let quots = [
  'quot1',
  'quot2',
  //...
];

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

CodePudding user response:

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 >Change</button>

<p>A Simple Text.</p>

  • Related