0

i am learning Jquery and i can't figure out why none of the JQuery lines execute, while javascript lines do. When I open it whith browser, the console shows"hello world" and "undefined". If i put these lines in the console, they are executed as expected. here is my code :`

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title> cim </title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> $('div:nth(0)').attr("class","marked") </script>
    <script>
        $(document).ready(funk())
        function funk() {
        $("div").css("border", "3px solid red");
            console.log("hello world");
            $("div:nth(0)").attr("class","marked");
            console.log($("div:nth(0)").attr("oo"));
            $("div").append("LLLLLLLLLLLLLLLLLLLLL");
        }
    </script>


</head>
<body>
<div oo="ooo">
<p> blah </p>
<p> blah <br></p>
<p>blah</p>
</div>
<div>
    <p> asd</p>
    <p> asd</p>
    <p > asd</p>
</div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</body>
</html>enter code here`
5
  • 2
    Why are you including 2 different versions of jQuery in your page? Commented Apr 9, 2016 at 21:38
  • 2
    Also: $(document).ready(funk); not funk() - you're calling the function when you include (). Commented Apr 9, 2016 at 21:38
  • I see </head> but no <head> Commented Apr 9, 2016 at 21:39
  • Also, don't put any code in the ></script> tags for the call to jQuery. Move $('div:nth(0)').attr("class","marked") to the script where the rest of your code is, within the ready function, Commented Apr 9, 2016 at 21:39
  • i deleted "()", works fine, thanks! Commented Apr 9, 2016 at 21:42

2 Answers 2

4

A few of issues

  1. You are including two versions of jQuery (no need for that)
  2. You cannot run code and link to an external file in one script element. So the code inside the first one will be ignored
  3. you immediately execute the func method and the DOM is not ready so all the elements are not found
  4. there is not :nth selector. Either :nth-child or :eq

What you need is

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
    $(document).ready(funk);
    function funk() {
        $("div").css("border", "3px solid red");
        console.log("hello world");
        $("div:nth(0)").attr("class","marked");
        console.log($("div:eq(0)").attr("oo"));
        $("div").append("LLLLLLLLLLLLLLLLLLLLL");
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

change $(document).ready(funk()) to this $(document).ready(function () {funk();});

1 Comment

Or just $(document).ready(funk). There’s no point in over-complicating it.

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.