• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Passing a div into a function

Im trying to get a simple function working, which will accept 2 arguments, one being the page to load, and the other being the divID to load the result into.

My function so far is setup as:-
        function ajaxDo($strPage,$strDiv) {
                $($strDiv).html("Loading...");
                $.ajax({
                    type: 'GET',
                    url: $strPage,
                    dataType: 'html',
                    success: function(retHtml, textStatus) {
                        $($strDiv).html(retHtml);
                        },
                    error: function (xhr, textStatus, errorThrown) {
                        $($strDiv).html("Failed - " + errorThrown ? errorThrown : xhr.status);
                        }
                    });  
            }

Open in new window


So, if I had my div setup as:-
            <div id="contentBody">
            </div>

Open in new window


I want to be able to use:-
                        <script type="text/javascript">
                            $(document).ready(function() {
                                $('#cmdMembers').click(function() 
                                    { 
                                        ajaxDo("_ShowMembers.php",'#contentBody');               
                                    });
                            });
                        </script>
                        <div class="cmdMenu">
                            <div id="cmdMembers" class="cmdMenu txtMenu">Members</div> 
                        </div>  

Open in new window


My issue is that I dont seem to be able to pass my #contentBody correctly into my function.

Any ideas?

Thanks in advance
0
tonelm54
Asked:
tonelm54
  • 2
1 Solution
 
ChrisCommented:
Try passing your div as an object. So call ajaxDo("_ShowMembers.php",$('#contentBody')); instead.

In this instance your function should be as follows:

        function ajaxDo($strPage,$strDiv) {
                $strDiv.html("Loading...");
                $.ajax({
                    type: 'GET',
                    url: $strPage,
                    dataType: 'html',
                    success: function(retHtml, textStatus) {
                        $strDiv.html(retHtml);
                        },
                    error: function (xhr, textStatus, errorThrown) {
                        $strDiv.html("Failed - " + errorThrown ? errorThrown : xhr.status);
                        }
                    });  
            } 

Open in new window

0
 
haloexpertsexchangeCommented:
You shouldn't need to add the $ in front of the  variable names $strPage,$strDiv, should work just as strPage, strDiv
Try changing that and see what happens.
0
 
ChrisCommented:
OK. Tested this for you. It's all working. http://jsfiddle.net/zdsr6/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now