Skip to content
Advertisement

Get specific tag values using jquery

I am trying to get “Pen” , “All Pen” values.

Here is what i tried :- Please take a look at the code & share your thoughts Please take a look at the code & share your thoughts

JavaScript
JavaScript

Advertisement

Answer

Let’s first try to fix your HTML. Considering I don’t know what your program or site does I just refactor everything you had:

JavaScript
JavaScript
JavaScript

In the script section, you have an example of how you can get the requested values. I separated them using the find() function to give you more control & understanding of how to reach them. I have to be honest with you. If you aren’t planning to support IE then using jquery only slows you down. You could have used querySelector() & querySelectorAll() instead.

Secondly, try to get data from text nodes is a really bad practice. If you want a value that is altered for the user you could have used an attribute to store that data in the given element or using the <data> element from HTML5. You could also just have wrapped it into a <span> tag to make sure you selected the correct element.

Good luck

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement