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()
});
4
  • 1
    get rid of the . between $ and your selector, for starters. Commented Apr 5, 2012 at 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. Commented Apr 5, 2012 at 20:56
  • what are you appending? Why the dots? What does page 2 has to do with this? Commented Apr 5, 2012 at 20:57
  • you cannot append from one document to another without ajax. Commented Apr 5, 2012 at 20:58

4 Answers 4

5

the problem

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

remove the .

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

Comments

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.

Comments

0

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

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

Comments

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.

Comments

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.