-2

I got a button name "modify" and when clicked got the value of it by the following

 $('button[name=modify]').click(function() {

        alert("button value that you click "+$(this).val());
        mybuttonvalue=$(this).val();
        alert("mybuttonvalue @ button click "+mybuttonvalue);
        $( "#mydialog" ).dialog( "open" );

    })

and the variable called "mybuttonvalue" is declared after document is ready and used at the model dialog form' open method as follow

  open:function(){
                alert(mybuttonvalue);
                alert($('#ztitle'+mybuttonvalue).val());
                alert($('#znews'+mybuttonvalue).val());
            $('#title').val($('#ztitle'+mybuttonvalue).val());              
            $('#news').val($('#znews'+mybuttonvalue).val());
        },

at this point the value of the variable is always 0. can someone point me out where did i make wrong. plz? thanks

3
  • Where are you setting the value of the button? Show us your HTML. Commented Jan 8, 2013 at 18:20
  • $msg.=" <div id='divPostControl'> <button id='modify' name='modify' value='".$newsid."' class='ui-button ui-widget ui-state-default ui-corner-all'>Modify</button> <button id='delete' name='delete' value='".$newsid."' class='ui-button ui-widget ui-state-default ui-corner-all'>Delete</button> </div>"; this is part of html form and it is repeating. Commented Jan 8, 2013 at 18:28
  • @yas You can edit your post and add that in there with formatting so looks better :) Also can you show the generated HTML Commented Jan 8, 2013 at 18:30

2 Answers 2

1

Do not set variables in one function and use them in another.

Instead, query the value in the function where you need it:

$('button[name=modify]').click(function() {
  $( "#mydialog" ).dialog( "open" );
});

and

open:function(){
  var mybuttonvalue = $('button[name=modify]').val();

  alert($('#ztitle'+mybuttonvalue).val());
  alert($('#znews'+mybuttonvalue).val());

  $('#title').val($('#ztitle'+mybuttonvalue).val());              
  $('#news').val($('#znews'+mybuttonvalue).val());
},

Another variant of doing it is discussed here: jquery-ui, Use dialog('open') and pass a variable to the DIALOG

Sign up to request clarification or add additional context in comments.

Comments

1

try this:-

open:function(){
                alert($('button[name=modify]').val())
            $('#title').val($('#ztitle'+mybuttonvalue).val());              
            $('#news').val($('#znews'+mybuttonvalue).val());
        },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.