Shubham Shrivastava
Shubham Shrivastava

Reputation: 47

how to append button with different name

I am trying to append button as btn-1 btn-3 btn-3 and but it's not working as I want how to solve it,

my code

html

<div id="press"> Click Me </div>
<div id="phaseDiv"></div>

jquery

$(document).on('click', '#press', function(){
        a=+1;
        a++
        $('#phaseDiv').prepend('<a style="background:blue; margin-left:5px; padding:5px; color:white;"> btn</a> ' + a )
        
   })

Upvotes: 1

Views: 88

Answers (1)

Swati
Swati

Reputation: 28522

Declare a globally and just increment it's value everytime you click on press button.

Demo Code :

var a = 0;//declare globally
$(document).on('click', '#press', function() {
  a++//increment
  $('#phaseDiv').prepend('<a style="background:blue; margin-left:5px; padding:5px; color:white;"> btn ' + a + '</a> ')

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="press"> Click Me </div>
<div id="phaseDiv"></div>

Upvotes: 1

Related Questions