Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Mobile Drop Down Menu Perform: Part 1

Kevin Faust
Kevin Faust
15,353 Points

some questions on .val( ) and trying to break things down slower

im sorta confused on a few steps and i hope that someone can assist me in breaking down the code

so basically first we cycle through each menu anchor link with the .each() method. then for each anchor link we peform what is inside the function. in our case, first it creates a $anchor variable inside the function scope and gives it the value of $(this). so the example ill use here will be <a href="about.html">About</a>. and then we create another variable called $option within the function scope. but since the $anchor variable also contains html markup which we dont want, we use the .text() method to assign the $option variable [href="about.html"About]. basically now we removed the markup through the text method. however we only want the href attribute so we use the .attr () method to grab the href and assign it as the new $option variable. im not sure where .val() fits into all of this...and then we finally put this href into the select tag.

but when we use the attr method and tell it to grab href, what part of href is it grabbing. im pretty sure it grabs this entire thing: [href="about.html"About] for example. but how come in the dropdown it doesnt show the about.html part and only the about text?

  1. Why can't we add [.attr("href")] after we write [var $anchor = $(this)]. we basically just want to cycle through each menu anchor element and we just want the href part. then we could just eliminate [$option.val($anchor.attr("href"))]. I tried it but it didn't work so im curious as to why we cant do that.

  2. also i tried this and it sorta worked. why would letting [$option.text($anchor.text())] be [$option.text($anchor.attr("href"))] not work? we essentially take the href from the anchor variable in 1 go and add it to the option variable. then we dont have to use .val() im still hazy on .val()....but in the end the drop down menu only returns the index.html/about.html/etc. part of the href

  3. i removed [$option.val($anchor.attr("href"))] from the code and it still works perfectly fine. What exactly does this line do. i know the href takes the href attribute from the $anchor variable but i still dont get what [.val()] does.

.

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

since im doing jquery again, i am able to see some of my old posts from months and months ago. it feels good to see how far ive progressed. although i quit jquery since i wrote that ^^, after doing other programming languages and gaining expose, i can actually follow jquery this time around. still a challenge though as im not proficient in any sort of front end web

I'm right there with you keven. I was very discouraged the first time through this course. I quit and followed w3schools jQuerry course. It's just basics, no applications but it helped. I'm back here, it's still easy to miss little details, but I'm getting the concepts better.

Polly Walter
Polly Walter
2,494 Points

glad others feel the same way. i am very discouraged by this course, i don't think its basic enough for first timers. while i prefer treehouse to codecademy for most of their courses, i think this is one that codecademy actually did better. i felt like i had much more of a grasp of basics after that course and thought this would be a good next step, but i just feel lost.

Polly, I like the layout of codecadamy and the speed of the lessons, but it seems like Treehouse goes way beyond what codecadamy does. Treehouse gets a lot harder exponentially. I go to codecadamy when I need a break. I can still learn without banging my head against the wall. I love both.