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
Mauro Bonucci
5,953 PointsAppend info from a div class to a hidden input value field
Hello, i have a div class that gives me a value, in my case a number, and i need to put this value inside a input hiddel value field, can someone tell me how do i make this?
1 Answer
Tim Rourke
12,151 PointsIf you're using jQuery, you can do it easily like this:
http://codepen.io/timothyrourke/pen/wotrc
Inspect the element with your web inspector of choice and see that the hidden input value equals whatever is the second class for the element.
Note that this approach only works if you know
A) how many classes are applied to the element you're targeting
and
B) if there ARE multiple classes for that element including the number you'd like to add as the hidden input value, you'll need to always be sure you know what order those classes are listed in
If you don't know how many classes are added, but you can be sure that there will be only one class that is the number you're looking for (highly likely) you can test to see if a variable is a number by doing this:
//where the variable 'x' is what you're testing:
if (typeof x === 'number') {
//do stuff
} else {
//do other stuff
}