1

I Have an anchor tag like this:

   <a href="secondPage.html" id="seeVid">
        <div class="box" style="margin-top:100px;" id="bee">
            <center><h2>Java</h2></center>
            <center><p>Hello paragraph for .</p></center>
        </div></a>

When I click it I want to go to my second page and append a paragraph within that div how do I accomplish this here's my jquery but it doesn't seem to work?

$(document).ready(function(){

    $("a#seeVid").click(function(){

        $('div.allVid').append("<p>Second Paragraph</p>");     

    });           
});

Here I am trying to see when my anchor tag with the id "seeVid" is clicked and then I go to my div in my second page with class "allVid" and I want to append a paragraph, but this isn't working?

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • 2
    Once you leave the page, the javascript is lost, it doesn't work for the next page you navigate to. – adeneo Jul 09 '14 at 22:18
  • even if the scripts are on their own js page and all my pages have a `` tag link to link them together??? – user3822291 Jul 09 '14 at 22:22
  • It doesn't work like that. Each page load gets a fresh copy of the included scripts and they don't share any state with each other. – mclaassen Jul 09 '14 at 22:25
  • Ill post a solution tomorrow. Got that codes here for you ... but WM is running. Cheers. – lin Jul 09 '14 at 22:26
  • The easiest way I can think of would be to use a cookie, and not the kind with chocolate chips. – ArtisticPhoenix Jul 09 '14 at 22:28

2 Answers2

0

You can't do that. When the second page loads all the javascript from the first page no longer exists nor will be executing.

You could just execute a javascript function when the anchor is clicked and have that function redirect to a URL adding a query string parameter of some data you want to send and then in the 2nd page load you could execute a javascript function to retrieve the query string parameters and do some operation with them. You can read how to redirect to anew URL using javascript here: How to redirect to another webpage in JavaScript/jQuery? and how to retrieve query parameters here: jquery get querystring from URL

Community
  • 1
  • 1
mclaassen
  • 5,018
  • 4
  • 30
  • 52
-1

You would not be able to append the div if the anchor tag is directing to a new page. You need to send in the text as a querystring to the new page and then using javascript append that in your newly rendered html

or

better still set a localStorage variable and in the new page script check the localStorage whether it is not empty and if it is not, append that so that it is included in your new page

More info about local storage:

W3School Explanation

A nice blog on it

V31
  • 7,626
  • 3
  • 26
  • 44