0

I'm trying to develop my skills with Ajax I want to change my wrapper content with a different pages wrapper content without having to refresh the page.

I'm using:

$(function () {
    $("#page2btn").click(function () {
        $.ajax({
            url: 'page2.asp',
            data: { id: $(this).attr('id') },
            success: function (data) {
                $('#Wrapper').html(data);
            }
        });
    });
});
<div id="Wrapper">
    <div id="menu">
        <a class="navbutton" onclick="MakeRequest1()">Home</a> <span class="navbutton">|</span>
        <a class="navbutton" id="Page2btn">Page2</a> <span class="navbutton">|</span>
        <a class="navbutton" onclick="MakeRequest3()">Page3</a>
    </div>
    <h1>Test1</h1>
</div>

and another page called page2.asp with a different wrapper content saying Test2 But my wrapper wont change when I click the button. any help would be appreciated.

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
ByronMcGrath
  • 365
  • 2
  • 3
  • 20

4 Answers4

1

Try to simplify it to determine if this is a problem with your front-end or back-end code...

Try this and see of it works any better..

$('#Wrapper').load('page2.asp?id='+$(this).attr('id'));
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • Well, that's the most very basic way to load content into a div. Are you getting an error? Have you ensured you are capturing the ID correctly? – Dutchie432 Jun 28 '14 at 10:48
1

Try this:

$(function () {
    $("#page2btn").click(function () {
        $.ajax({
            url: 'page2.asp',
            data: { id: $(this).attr('id') },
            success: function (data) {
                $('#dynamic_content_div').html(data);//data = <h1>Test 2</h1>
            }
        });
    });
});

HTML:

<div id="Wrapper">
    <div id="menu">
        <a class="navbutton" onclick="MakeRequest1()">Home</a> <span class="navbutton">|</span>
        <a class="navbutton" id="Page2btn">Page2</a> <span class="navbutton">|</span>
        <a class="navbutton" onclick="MakeRequest3()">Page3</a>
    </div>
    <div id="dynamic_content_div">
    <h1>Test1</h1>
    </div>
</div>
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
1

Javascript is case sensitive. You are using lowercase in case of assigning the event -

$(function () {
    $("#page2btn").click(function () { //lowercase P
        ...
    });
});

but using uppercase for html id

        ...
        <a class="navbutton" id="Page2btn">Page2</a> <span class="navbutton">|</span> //upper case P
        ...

so, it never finds the item to bind to, thus nothing happens when you click. Either change js to -

$("#Page2btn").click(function ()

or change html to -

<a class="navbutton" id="page2btn">Page2</a> <span class="navbutton">|</span>

Other than this, I don't see the error.

brainless coder
  • 6,310
  • 1
  • 20
  • 36
1

Try jQuery replaceWith method, like this:

$(function () {
   $("#page2btn").click(function () {
      $.ajax({
          url: 'page2.asp',
          data: { id: $(this).attr('id') },
          success: function (data) {
              $("#Wrapper").replaceWith(data);  //try with double qoutes
          }
      });
   });
});

*The link below maybe helpful:

Change content of div using jQuery

Community
  • 1
  • 1
Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29