Jump to content

Retrieve element attribute from modal window upon $_GET request using jQuery


Recommended Posts

I'm using the jQuery UI interface for my design, and I have a (slightly trivial) question on how to do this. It isn't crucial, but for my own edification (and coolness points) I'd like to know. Essentially, I have the following function to load a page in a modal window:

    function showDialog(qs,processFile,h,w,form)
    {
        var tag=$("<div id='userfrm'></div>");
        $.ajax({
            type:"GET",
            url:processFile,
            data:qs,
            success: function(data){
                tag.html(data).dialog({
                    height:h, width:w, modal:true,
                    buttons:{
                        "Update":function(){
                            var string = $("#"+form).serialize();
                            $.post(processFile, string,function(data){
                                var content = $(data);
                                $("#userfrm").empty().append(content);
                            });
                        },
                        "Cancel":function(){
                            $(this).dialog('close');
                        }
                    },
                    close:function(){
                        $(this).dialog('destroy').remove();
                    }
                }).dialog('open');
            }
        });
    }

Now according to the documentation, the title can be set via

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

, which seems trivial. However, since the title depends on the page which is being loaded, for now I have the would-be title as a title attribute on a form. I'd like to know how to retrieve this attribute via JS and dynamically set it as the modal's window title. For now I figure it's something like (under the 'success' parameter for the $.ajax request):

var title=$(data).("#"+form).attribute('title')

I believe this goes something like "go into the data returned by the server, get the form with this id and retrieve the title attribute from it". Clearly the syntax is wrong (or I would not be posting), so if someone can give me the nudge that I need I'll be forever in yer debt  ;)

If you want attribute try $('#form').attr(); not attribute

Hi,

Sorry, I'm aware that I misspelled the attr() function. My issue is that $("#form") isn't yet in the DOM, so it isn't accessible directly and I need to access it via $(data). I've tried $(data).find("#"+form+"[attribute=title]") and it also did not work.

I did it! This is how:
[quote]    function showDialog(qs,processFile,h,w,form)
    {
        
        var tag=$("<div id='userfrm'></div>");
        $.ajax({
            type:"GET",
            url:processFile,
            data:qs,
            success: function(data){
                tag.html(data).dialog({
[b]                    title:function(){
                        return $("#"+form).attr('title');
                    },
[/b]                    height:h, width:w, modal:true,
                    buttons:{
                        "Update":function(){
                            var string = $("#"+form).serialize();
                            $.post(processFile, string,function(data){
                                var content = $(data);
                                $("#userfrm").empty().append(content);
                            });
                        },
                        "Cancel":function(){
                            $(this).dialog('close');
                        }
                    },
                    close:function(){
                        $(this).dialog('destroy').remove();
                    }
                }).dialog('open');
            }
        });
    }

The part that I needed is in bold. Turns out, after I do tag.html(data), the data is accessible. Then I can just declare a function and access directly!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.