20

I have the following code:

<div id="DivPassword" title="test" > 

I want to change the div title and I have the following code:

 function ChangeAttribute() {
         $("#DivPassword")
            .attr('title', 'Photo by Kelly Clark');
         $('#DivPassword').dialog('open');
         return false;
     }

When the dialog is opened, the title is still test! if I don't assign any title to the div, the dialog doesn't show any title. How can I correct that?


 function ChangeAttribute() {
         $("#DivPassword")
            .attr('title', 'Photo by Kelly Clark')
            .dialog('open');

         alert($("#DivPassword").attr('title'));
     }

$('#DivPassword').dialog({
             autoOpen: false,
             width: 800,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                 alert($(this).attr('title'));
                     $(this).dialog("close");
                 }
             }
         });

 <script type="text/javascript">
     var Dtitle;
     $(function() {
        $('#DivPassword').dialog({

             autoOpen: false,
             width: 800,
             title : Dtitle,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                     $(this).dialog("close");
                 }
             }
         });
     });

     function ChangeAttribute(name) {
         $("#DivPassword")
            .attr('title', name)
            .dialog('open');
         Dtitle = $("#DivPassword").attr('title');
         alert(Dtitle);
     }


</script>

9 Answers 9

25

You can change the title of a dialog directly with:

$('#DivPassword').dialog('option', 'title', 'Photo by Kelly Clark');

This will work in your case. In fact, your code to change the title attribute is correct. I guess that the dialog plugin creates the dialog when .dialog is called first. The open method just displays the dialog then, without re-creating it from the div.

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

3 Comments

I have changed the code as in the edited part above. Still, I cant figuire out why its not working.
You still don't use the dialog-method to set the title. You can add my line right before your alert-statement and it should work.
works perfect - I was trying all these other ways from different posts & articles. This is clearly the most elegant solution .. aka, the best! I have been learning/working with jquery dialogs for a few months now, and only seeing this way of doing things for the first time here. Not sure why the docs, tuts, & posts don't emphasize this type of solution more since it is very useful/powerful in many different code situations .. ? .. Big thanks for sharing a great one-liner
18

You can change ANY attribute from any DOM element with jQuery as easy as:

$("#divMessage").attr('title', 'Type here the new Title text');

Cheers!

Comments

1

Have you tried any of the suggestions given here: How to change an element’s title attribute using jQuery?

It looks like you are doing what the accepted answer suggests. If this doesn't work, perhaps the others will.

Comments

1

Thanks for all the answers.

The $('#DivPassword').dialog({ had to be after .dialog('open');

The simplest way was to do as follows:

 $("#DivPassword")
            .dialog('open');
         $('#DivPassword').dialog({
             autoOpen: false,
             title: $('#DivPassword').attr('title') + name,
             width: 400,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                     $(this).dialog("close");
                 }
             }
         });

Comments

1
$("#div1").dialog({ autoOpen: false, modal: true, height: 420, width: 750, resizable: false });
// more code
$("#div1").dialog("option", "title", "joopla, here is a new title");

did the trick for me..

Comments

0

It sounds like your plugin is reading the title before it gets to that ChangeAttribute function. The code you've posted will certainly change the title attribute, so there's something else going on behind the scenes.

Also, while I'm here, don't forget to use chaining. Most functions in jQuery return the same object again:

$("#DivPassword").attr('title', 'Photo by Kelly Clark').dialog('open');

// or like this, perhaps easier to read:

$("#DivPassword")
    .attr('title', 'Photo by Kelly Clark')
    .dialog('open')
;

1 Comment

I have tried to add alerts...as in the edited code above. But though bothalerts showing the right title, the title isnt being displayed.
0

Try setting the title attribute after the dialog is open. Probably your dialog.open() will be resetting the title attribute.

I think you can supply a callback function for the open event.

See open

1 Comment

Had it been resetting, then I dont think the inital title would have been displayed! What do you think about that? The new title that is being assigned isnt being displayed.
0

Specifies the title of the dialog. The title can also be specified by the title attribute on the dialog source element.

Code examples

Initialize a dialog with the title option specified.

$( ".selector" ).dialog( { title: 'Dialog Title' } );

Get or set the title option, after init.

//getter
var title = $( ".selector" ).dialog( "option", "title" );
//setter
$( ".selector" ).dialog( "option", "title", 'Dialog Title' );

source.

to answer your the dialog doesnt show any title.

Comments

0

i did this:

$("#div1").dialog({ autoOpen: false, modal: true, height: 420, width: 750, resizable: false });
//more code
$("#div1").dialog("option", "title", "joopla, here is a new title");

This works for me

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.