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.
johnsonallstarsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
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.
0
RobOwner (Aidellio)Commented:
You don't need an auto-increment.  

I've modified your code slightly (code and demo below).  I've added the "dialogmsg" class to the dialog markup.  I've added the "data" attribute to the buttons to store the dialog they're related to.
You'll see in the javascript that you don't need to remember the dialog as an object as it is attached to the markup in the background so I've initialised the dialogs using the class i.e. $( '.dialogmsg' ).dialog({...})
Same goes for the buttons (with the btndialog class added): i.e. $('button.btndialog').on('click',function() { ... })

demo: http://jsbin.com/miqahiqiqo/1/edit?html,js,output

js:
$(function() {
  $( '.dialogmsg' ).dialog({
    autoOpen: false,
    height: 300,
    width: 350,
    modal: true,
    buttons: {
      Cancel: function() {
        $(this).dialog( 'close' );
      }
    },
    close: function() {
    }
  });


  $( 'button.btndialog' ).button().on( 'click', function() {
    var target = $(this).data('target');
      $("#"+target).dialog( 'open' );
  });
});

Open in new window


html
<!--//the 1st message works with jQuery dialog-->
<div id='dialog-message1' class='dialogmsg' title='Explanation'>Some information for the 1st message</div>
  <button id='create-explanation1' class='btndialog' data-target="dialog-message1">Explanation</button>

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shano KhanSoftware EngineerCommented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.