arenaninja Posted April 10, 2012 Share Posted April 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260662-retrieve-element-attribute-from-modal-window-upon-_get-request-using-jquery/ Share on other sites More sharing options...
caliux Posted April 10, 2012 Share Posted April 10, 2012 If you want attribute try $('#form').attr(); not attribute Quote Link to comment https://forums.phpfreaks.com/topic/260662-retrieve-element-attribute-from-modal-window-upon-_get-request-using-jquery/#findComment-1335966 Share on other sites More sharing options...
arenaninja Posted April 10, 2012 Author Share Posted April 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260662-retrieve-element-attribute-from-modal-window-upon-_get-request-using-jquery/#findComment-1335971 Share on other sites More sharing options...
arenaninja Posted April 10, 2012 Author Share Posted April 10, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/260662-retrieve-element-attribute-from-modal-window-upon-_get-request-using-jquery/#findComment-1335979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.