Javascript Change text when button is clicked -


hi want change text inside div when button click.

<div>     <p id="part1">hello</p>      <button type="button" onclick="myfunction()">next</button> </div>    <script> function myfunction() { document.getelementbyid("part1").innerhtml="world"; } </script> 

this works perfectly. have div has text , button inside, in case has word "hello". code have when click button word "hello" change word "world". want make sequence change word "hello" "world", "world" "welcome" , "welcome" "my program" (example). , clicking button multiple times.

if looking text change cycle happen try this:-

var textarray=['world', 'my', 'welcome', 'something','hello'] function myfunction() {     var value = textarray.shift(); //get first item array     textarray.push(value); //push cycle repeat.     document.getelementbyid("part1").innerhtml=value; } 

fiddle


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -