1

This my current code:

var container, url;

$(".nav-list li a").on("click", function(e) {

   e.preventDefault();

   var $url = this.href + " #content";
   var container = $("#content");

   $(".nav li").removeClass("active");
   $(this).parent().addClass("active");

   $(this).addClass("clicked");

   var $row = $(".span10");
   var $rowNew = $('.new');

   if ($rowNew.children().length > 1) {
        $(".span10 div").removeClass("new");
        $('<div class="row-fluid new"></div>').prependTo($row);
   }

   if ($(".new").length == 0) {
        $('<div class="row-fluid new"></div>').prependTo($row);
        $('<div class="span6">' + container.load(url) + '</div>').appendTo('.new');
   } else {
        $('<div class="span6">' + container.load(url) + '</div>').appendTo('.new');
   }

});

I am trying to load content inside <div class="span6">HERE</div> but I am getting confused on how to set the .load and load the content in it.

Current html output:

<div class="row-fluid new">
   <div class="span6">
       container.load(url)
   </div>
</div>
8
  • I guess the row var url = this.href + ".content"; wont work as this.href in the jquery context should be $(this).attr('href') Commented Feb 7, 2013 at 21:31
  • WHy you have var container, url; and than again var url = this.href + ".content"; var container = $("#content"); Commented Feb 7, 2013 at 21:31
  • Have you tried loading first, and then use container.html()? Commented Feb 7, 2013 at 21:32
  • @roXon var container, url; was a typo, thanks Commented Feb 7, 2013 at 21:34
  • @jtheman changed $(this) and updated the question with th current output Commented Feb 7, 2013 at 21:35

1 Answer 1

2

I do not think you should use load this way; it is only meant to place the result into an existing element that you know beforehand. Better use get with a success callback:

    e.preventDefault();

    var url = $(this).attr('href') + ".content";

    [...]

    $.get(url, '', function(result) {
      if ($(".new").length == 0) {
        $('<div class="row-fluid new"></div>').prependTo($row);
        $('<div class="span6">' + result+ '</div>').appendTo('.new');
      } else {
        $('<div class="span6">' + result + '</div>').appendTo('.new');
      }
    });

No need to use the var container = $("#content"); and remove its id in the process.

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

8 Comments

Nothing is load, not even adding the new divs
this is what i did and i get nothing at all, not even new divs pastebin.com/27DCXS8n
Ah, I see that url is undefined. If you want to use the href of the currently clicked link, use var url = $(this).attr('href'); instead of the current var $url = ... (just var url = this.href might usually work, too)
yes i changed it to var url = this.href + ".content"; but it doesn't load the content, what it does is it replaces the whole thing. I don't see the new divs created
ah, phew, thanks, curiously I had a similar problem with the $ just a few hours ago ;)
|

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.