0

I try to append something to a div over a Php echo

Includes

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

Php Code

<?php echo "<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>"; ?>

My Div Code

<div class="notificationsBody">TESTING DIV<br></div>

Is this possible and if yes why isnt this working ? My Console is empty too. If not is there a other way to realize a Div appending over a Php Echo ?

Hint: I need a Php echo if its possible.

This is my complete index.php Page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        "http://www.w3.org/TR/html4/loose.dtd">



<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>
<body>


<?php echo "<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>"; ?>
<div class="notificationsBody">TESTING DIV<br></div>

</body>



</html>

What i want at the end is like you have a Class and call the Function

<?php echo $myclass->theechofunction(); ?>

Then this will echo the Javascript which appending a Div to my Wrapper.

Yes its a bit crazy / difficult but thats what i need.

Thanks for every Answer.

EDIT

The Site Quellcode after calling the site

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>



<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>

<div class="notificationsBody">TESTING DIV<br></div>
12
  • 1
    Why do you need a PHP echo? Commented Sep 4, 2015 at 22:18
  • 1
    Looks like you're running it before the page loads learn.jquery.com/using-jquery-core/document-ready Commented Sep 4, 2015 at 22:20
  • 1
    I testing at the moment some things :) And i need Php echo for it Commented Sep 4, 2015 at 22:20
  • Can you explain your ultimate intention because the example you've given is quite odd. Commented Sep 4, 2015 at 22:24
  • 1
    Please show the outputted HTML in the question Commented Sep 4, 2015 at 22:26

1 Answer 1

3

You have two problems. First, you're loading two versions of jQuery, that will probably cause conflicts. Either load jQuery from ajax.googleapis.com or code.jquery.com, but not both.

Second, the Javascript that tries to append to .notificationBody is running before you add that element to the DOM. There are two ways to solve this: either move the code that echoes the <script> tag to after you echo the .notificationBody DIV, or run your code in the $(document).ready() handler, which runs after the DOM is completely loaded:

<?php echo "<script type='text/javascript' >
    $(document).ready(function() {
        $('.notificationsBody').prepend('Hello World');
    });
    </script>";
?>

See JS & jQuery can't detect html elements, and say's they are undefined

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

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.