0

I am trying to set up a simple jquery for my symfony platform. Just trying to implement this http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_p to see if everything works fine.

base.html.twig

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>{% block title %}TEST JQUERY{% endblock %}</title>
    {% block stylesheets %}{% endblock %}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
    {% block body %}{% endblock %}
    {% block javascripts %}{% endblock %}
</body>
</html>

index.html.twig

{% extends '::base.html.twig' %}

{% block body %}

    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                $("p").hide();
            });
        });
    </script>


<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>


{% endblock %}

when i do view source on the page, the structure seems fine but the jquery is not being executed.

1
  • Did you check the console of your browser web tools? Is the Jquery file included? Maybe some other errors are echoed? Commented Feb 5, 2013 at 10:36

3 Answers 3

2

Your jQuery you are including doesn't exist on google's CDN

Use <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> instead.

If you are wanting version 1.9.1, get if from jquery.com at this link

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

2 Comments

thank you for pointing out this.. i found the jquery link from the jquery.com/download page.. which displays as script src="ajax.googleapis.com/ajax/libs/jquery/1.9.1/…> .. i should have paid more attention.. Thanks again
Ah okay, I just noticed it too. Maybe Google will update their CDN soon.
1

Change the jquery function and try this:

<script type="text/javascript">
    $(document).ready(function(){
        $("button").live('click', function(){
            $("p").hide();
        });
    });
</script>

update

As rrikesh pointer out since live is deprecated after v1.7, you may want to use this:

<script type="text/javascript">
    $(document).on("click", "button", function (){
        $("p").hide();
    });
</script>

3 Comments

As of jQuery 1.7, the .live() method is deprecated
actually in 1.9 (which he's using) is removed, not just deprecated
Thanks for fixing, but I think the problem here is a bad link to jquery as I mentionned. I got a 404 trying to access it and I just checked: Google CDN stops at version 1.9.0
0

This could be a better solution:

<script src="{{ asset('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js') }}"></script>
<script><window.jQuery || document.write('<script src=\"\{\{ asset(\'../app/Resources/public/js/vendor/jquery-1.9.1.min.js\') \}\}\"><\/script>')</script>

And download the jquery-1.9.1.min.js file to your app/Resources/public/js/vendor/ directory.

In this way, the client always gets the source.

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.