Appending JavaScript

Go To StackoverFlow.com

0

I am am trying to append an element to a <div> of a page, but it is not working.

Page1 (index.php)

<a href=" another.php " id="p1id" type="button">Go to page 2</a>

Page2 (another.php)

<div id=p2id></a>

JavaScript (jqtest.js)

$.("#p1id").click(function(){
    $.("#p1id").append()
});
2012-04-05 20:52
by tawheed
get rid of the . between $ and your selector, for starters - Mathletics 2012-04-05 20:55
In Page2, you're closing a <div> tag with </a>. That's invalid. Also, just calling the append method with no argument is meaningless - FishBasketGordo 2012-04-05 20:56
what are you appending? Why the dots? What does page 2 has to do with this - DA. 2012-04-05 20:57
you cannot append from one document to another without ajax - Fresheyeball 2012-04-05 20:58


5

the problem

$.("#p1id").click(function(){
-^here
 $.("#p1id").append()
--^ here
});

remove the .

2012-04-05 20:56
by Rafay


2

try this:

<div id="p2id"></div>

...

$("#p1id").click(function(event){
    $("#p1id").append();
    event.preventDefault();
    return false;
});

In addition:

  1. You need to append something
  2. It looks like you are using an anchor, <a/>, as a button. You will need to prevent the default action, which would be to load a page.
2012-04-05 20:56
by Joe


0

You're also calling append() with no arguments. It needs a content argument.

http://api.jquery.com/append/

2012-04-05 20:58
by ceejayoz


0

If your html is

<div id="p1id">A div<div>​

You can append like

$(function(){
    $('#p1id').click(function(e){
        var myP=$('<p class="myClass">New paragraph</p>');
        $(this).append(myP);
    });
});​

An example is here.

2012-04-05 21:02
by The Alpha
Ads