Link to home
Start Free TrialLog in
Avatar of johnsonallstars
johnsonallstars

asked on

Auto increment in jQuery script

Hello,

I have working script for a jQuery dialog message.

Here's the code:
<script>
  $(function() {
    var dialog
 
    dialog = $( '#dialog-message1' ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        Cancel: function() {
          dialog.dialog( 'close' );
        }
      },
      close: function() {
      }
    });
 
 
    $( '#create-explanation1' ).button().on( 'click', function() {
      dialog.dialog( 'open' );
    });
  });

</script>

on the page results I have:
//the 1st message works with jQuery dialog
<div id='dialog-message1' title='Explanation'>Some information for the 1st message</div><button id='create-explanation1'>Explanation</button>

//as you can see the 2nd, 3rd, 4th messages will not work as I have the script above.
<div id='dialog-message2' title='Explanation'>Some information for the 2nd message</div><button id='create-explanation2'>Explanation</button>

How can I update or I perhaps auto increment the number in the script?  So, that each time I call the button the script will work for that message 1,2,3,4,5 etc.

Thanks in Advance.
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

What I would consider doing is instead of having several different diva that you open as the dialog. I would have your several different messages within different divs similar to what you have. Then I would have a JavaScript variable counting how many times the button is clicked. Then append the html of the div into your dialog.  

Something like.

Hello,

I have working script for a jQuery dialog message.

Here's the code:
<script>
  var count = 1;
  $(function() {
    var dialog
 
    dialog = $( '#dialogDiv' ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        Cancel: function() {
          dialog.dialog( 'close' );
        }
      },
      close: function() {
      }
    });
 
 
    $( '#create-explanation' ).button().on( 'click', function() {
      dialog.dialog( 'open' );
       count++;
       $('#dialogDiv').empty();
        var messageid = 'message' + count;
       $(messaged).appendTo('#dialogDiv');

    });
  });

</script>

<div id="dialogDiv"></div>

//the 1st message works with jQuery dialog
<div id='message1' title='Explanation'>Some information for the 1st message</div><button id='create-explanation1'>Explanation</button>

//as you can see the 2nd, 3rd, 4th messages will not work as I have the script above. 
<div id='message2' title='Explanation'>Some information for the 2nd message</div><button id='create-explanation2'>Explanation</button>

Open in new window


I didn't test out the code so sorry if there are any typos (I am on an iPad), but hopefully the idea comes across and helps.
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am also looking for this. But I have a one question:

1) I want to generate 'id' and 'data-target'  incrementally  and dynamically with jquery
so that I just need one line of code for <button> tag

How can I do this?