Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how jQuery works [closed]

Tags:

jquery

I'm new to jQuery. I'm taking a course and was given a test project, but I have no idea what to do. I'm just asking for someone to help me with an understanding. Here are the requirements:

Assignment

Here is what I tried:

$(document).ready(function() {
// site code
for (var i=0; i<10; i++)
{   
    //jQuery factory
    $("body").appendTo("<a href=">"+i+</>")
}
});

Here is the HTML section:

<body>
<div id="wrap">
    <ul id="navleft"></ul>
    <ul id="navright"></ul>
</div>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>

</body>
</html>

I don't want answers, but a push in the right direction. I need a tutor, but they are so expensive and my teacher is never to be found, so I'm seeing if someone wouldn't mind helping.

like image 982
Plextor3009 Avatar asked Apr 17 '13 17:04

Plextor3009


People also ask

How does jQuery function work?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

Is jQuery used in 2022?

And that brings the question: is it worth learning jQuery in 2022? It's good to know jQuery and there are still use cases for it. However, you should not spend a lot of time learning it. jQuery should not be your focus this year.

Is it still OK to use jQuery?

jQuery is still very popular there; it works and it's simple, so people don't feel the need to stop using it.” jQuery will continue to be a part of WordPress for some time to come, if for no other reason that it would be difficult to remove it without breaking backward compatibility.

How is jQuery executed?

The jQuery starts its code execution from the $(document). ready() function which is executed whenever the whole HTML DOM is loaded and is totally rendered by the browser, so that the event handlers work correctly without any errors. This $(document).


2 Answers

you have both logical and syntax errors. Make sure your statements end with a ';'

I think you are confused of how the $() works. In simple terms, you can put in the $() whatever you want to select from the page. So you are doing $('body'), which tells jquery, "hey give me back all of the elements that are tagged <body>", which is of course just the one.

Your assignment wants you to 'appendTo' the 'navleft' id. So what would you want to select from the page? Please note that to lookup an Id using $() you should do $("#id")

now for appendTo, you assignment says to append a <li>, however you are appending something else.

You should append EXACTLY what it says to append. <li><a>..<a/><li/>

The last traversing requirement you should have enough to do yourself. Just remember that $() will select all items that you tell it to, so what do you need to select to apply a color to all the anchors?

like image 143
75inchpianist Avatar answered Oct 28 '22 01:10

75inchpianist


Aside from the obvious syntax errors in your post, I would look at the documentation for appendTo and append.

... and then swap round "body" and "<a href=">"+i+</>"


You can track down syntax errors and runtime exceptions by using the Web Tools bundled with your browser. In most browsers (Chrome, FireFox) access these via F12 on your keyboard.

like image 32
Matt Avatar answered Oct 28 '22 00:10

Matt