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 trialja5on
10,338 PointsJavaScript in a table
Is their anything wrong with JavaScript inside a table? Its been suggested that its not ideal however to get it done another way I feel like the code is too much and way confusing, so do I have to take the week off or is this alright?
3 Answers
Armin Kadic
16,242 PointsHi jasonj7 !
While your code might work, I personally would use a different approach. Instead of adding the the script tag inside the table, I would simply add an id attribute to the table tag. The script tag would be before the closing body tag. With the id attribute in the table tag, you can now create a variable in the JS file and use document.getElementById to select it. Then simply call the getRandomNumber function on the variable you just created. This is just my personal opinion, I'm sure there are better ones out there.
Hope it helps :)
Good luck!
ja5on
10,338 PointsI did finish the DOM and JavaScript course but unfortunately it hasn't stuck - I have been right back to the beginning of the course once already but im having so many problems, making it all stick.
I tried to put how u said but its not making sense to me. My fault not yours.
Armin Kadic
16,242 PointsHey again jasonj7 !
Sorry, I don't fully understand your idea of how you want it to look like. I might not be the biggest help here, since I was away for some time and now I'm refreshing my memory again. There is many ways you could code one thing, you could create an entire table in javascript if you want, but that would take more code and you said you want to make it short. Go through the course again if needed, maybe you will find a missing part that you seek. Or maybe move on to the next project and come back to this one later? You should also take a break from time to time, because otherwise your brain will be overloaded and you won't be able to understand what's happening. Whatever you do, don't give up, remember why you started in the first place! Everyone struggles, no one knows everything, so don't give up on your hope jasonj7 !
ja5on
10,338 Pointsdoesnt matter ive deleted the program.
ja5on
10,338 Pointsja5on
10,338 PointsOh that is so much what I was hoping for thank you!
ja5on
10,338 Pointsja5on
10,338 PointsHope you don't mind my asking Armin Kadic but I have added an id="loopsToRun" inside the table tag, and I have put the script tag before the closing body tag, now I have written at the top of the JS file document.get.elementById(loopsToRun) - I know its wrong i just don't know what to do here, could you help?
Armin Kadic
16,242 PointsArmin Kadic
16,242 PointsI don't know if you've finished the "JavaScript and the DOM" course jasonj7 , but in case you didn't make sure you do it :) Guil explains everything nicely and easy to understand. You can also see how to do it properly on the mozilla developer network, that's what I do mostly when I need to refresh my memory. Here is an example of what I meant, you can change it however you want:
const tableId = document.getElementById("loopsToRun");
and then just use it as an argument to the function like "getRandomNumber(tableId)".
Here is a link on how to select an id properly:
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
Feel free to experiment with your code, I tend to learn better that way.
I'm really happy I could help :)
Good luck with your coding!