0

So I am trying to create a webpage that has a workout tracker on it. It starts with two weeks of input boxes that the user can put their exercise in. I want to make it to where if the user wanted to add a week, they could do so with just pressing the button. I have created this tracker in a form.

I have gotten the div to output but it doesn't output below the other input boxes, it instead outputs to a different page and I have to reload the page to get back to the tracker.

Here is the script that I have made so far:

                     <script>
                             var btn = document.getElementById("add")

                               function plusOne(){
                                var addWeek = document.getElementsByClassName("things");
                                 // This loop prints out the entire week
                                    for(var another of addWeek){
                                        document.write(another.innerHTML);
                                    }
                                }
                            //    This adds and event listener which when you press the button, it
                            // actives the plusOne function.
                               btn.addEventListener("click", plusOne);
                           
                            </script>`


To be honest with you, this is really above my weight class. I just started classes for web development during this spring. I have tried looking up the more advanced stuff but I don't understand it well enough to modify it for my needs. I have tried chatGPT and the code it gives for some reason is not working.

Here is the website link so that you can see the issue in action: https://belrossita.github.io/FrontEndDevAttempt2/

I only have the Training tracker worked on so far and very little styling. Thank you for you help.

This is one of the codes that chatGPT gave me that didn't work

     var addButton = document.ElementById("add");
                            addButton.addEventListener("click", addAWeek);

                            function addAWeek() {
                                var newWeek = document.createElement("section");
                                newWeek.innerHTML = "
                                <hr>
                                <h2>New Week</h2>
                                <div class="daystraining">
                                                <label for="days">&nbsp; </label>
                                                <label for="sunday">Sunday</label>
                                                <label for="monday">Monday</label>
                                                <label for="tuesday">Tuesday</label>
                                                <label for="wednesday">Wednesday</label>
                                                <label for="thursday">Thursday</label>
                                                <label for="friday">Friday</label>
                                                <label for="saturday">Saturday</label>
                                            </div>
                                            <div class="exercise2">
                                                <label for="rowOne">Enter Exercise Here:</label>
                                                <input type="text" name="postweekend" id="postweekend" >
                                                <input type="text" name="weekdayone" id="weekdayone" >
                                                <input type="text" name="weekdaytwo" id="weekdaytwo">
                                                <input type="text" name="weekdaythree" id="weekdaythree">
                                                <input type="text" name="humpday" id="humpday">
                                                <input type="text" name="preweekend" id="preweekend">
                                                <input type="text" name="weekend" id="weekend">
                                            </div>
                                            <div class="weight2">
                                                <label for="rowTwo">Enter Weight Here:</label>
                                                <input type="text" name="postweekend" id="postweekend" >
                                                <input type="text" name="weekdayone" id="weekdayone">
                                                <input type="text" name="weekdaytwo" id="weekdaytwo">
                                                <input type="text" name="weekdaythree" id="weekdaythree">
                                                <input type="text" name="humpday" id="humpday">
                                                <input type="text" name="preweekend" id="preweekend">
                                                <input type="text" name="weekend" id="weekend">
                                            </div>
                                            <div id="closeToEnd">
                                                <div class="reps2">
                                                    <label for="rowThree">Enter Reps Here:</label>
                                                    <input type="text" name="postweekend" id="postweekend" >
                                                    <input type="text" name="weekdayone" id="weekdayone">
                                                    <input type="text" name="weekdaytwo" id="weekdaytwo">
                                                    <input type="text" name="weekdaythree" id="weekdaythree">
                                                    <input type="text" name="humpday" id="humpday">
                                                    <input type="text" name="preweekend" id="preweekend">
                                                    <input type="text" name="weekend" id="weekend">
                                                </div>
                                            </div>
                                            <div id="lastRow">
                                                <div class="sets2">
                                                    <label for="rowFour">Enter Sets Here:</label>
                                                    <input type="text" name="postweekend" id="postweekend" >
                                                    <input type="text" name="weekdayone" id="weekdayone">
                                                    <input type="text" name="weekdaytwo" id="weekdaytwo">
                                                    <input type="text" name="weekdaythree" id="weekdaythree">
                                                    <input type="text" name="humpday" id="humpday">
                                                    <input type="text" name="preweekend" id="preweekend">
                                                    <input type="text" name="weekend" id="weekend">
                                                   
                                                </div>
                                            </div>";
                                            var outputDiv = document.getElementById("output");
                                            outputDiv.appendChild(newWeek);
                            }
  • ChatGPT answers can be helpful and sometimes give you fresh coding ideas but they are not a substitute for a proper learning journey into web coding with JavaScript. – Carsten Massmann Aug 14 '23 at 05:47
  • Is there anything wrong with the code that i created? The function plusOne()? is there any way i can get the div to post underneath the lastRow instead of on a blank page? – belita ross Aug 14 '23 at 07:07
  • Try looking up the documentation for `document.write`. One of its side effects is to overwrite the existing page if you you use if after the document has loaded. No idea where you got the script from. Edit: you got it from chatGP, the world famous paraphrasing nonsense generator :-) – traktor Aug 14 '23 at 07:40
  • Note also that chatChatGP provided invalid HTML - The ids of elements in the DOM should be unique - duplicating `id` values 5 times in the same week, then duplicating them all over again for additional weeks does not resemble the output of a programmer. I suggest writing _you own code_ from the beginning - if you get stuck you can always ask a new question about why it doesn't work as (you) intended. – traktor Aug 14 '23 at 08:13
  • I did write my own code. It is the smaller bit. I don't know much about DOMs as I said I am just getting started in coding. I am throwing together what little knowledge and reading I have done and am using all tools that I can, that include chatGPT. – belita ross Aug 14 '23 at 10:29
  • I had no intention of discouraging you. However using `document.write` to generate DOM content never worked for dynamic content (it only works during page load) and has been generally replaced by DOM methods to insert HTML as content into an element such as [`Element: insertAdjacentHTML()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML), and by setting element ' `innerHTML` and `textContent` properties. There are even more methods to create and move elements around the DOM. I do encourage you to learn more about the DOM and not to be misled by bad CGPT code. – traktor Aug 14 '23 at 11:22
  • @traktor Okay, thank you. I'll read up more about 'insertAdjacentHTML' and how to create methods to do what I want with this. I guess I just have to keep searching for the right information source that can teach this to a beginner. Thanks again. – belita ross Aug 14 '23 at 18:34

0 Answers0