Home > Software engineering >  Trying to describe div in JavaScript but did not do it
Trying to describe div in JavaScript but did not do it

Time:06-24

My project is to create website of 'Daily item Rate List' so i want to create different tabs like vegetables rate, fruit rate etc. So problem is i want to add card for different items like when i click on vegetables tab different types of vegetables appear on vegetables log. but i dont know how to define div in JavaScript. I want to create different function for different items like function vegetables(){ } and then inside function i want to place different card of different item like tomato rate card ,onion card etc so in short i want to do create such function and then call it by button just like we call normall function but i do not know to declare div and do such thing and how to call it so will you please help me out

CodePudding user response:

I think you're just looking for createElement():

document.createElement("div");

This is really Javascript 101, so I would recommend getting familiar with the basics.

w3schools - createElement()

CodePudding user response:

There are 2 basic options:

  • [discouraged]
    Use a script element in the html body <script>document.write("<div />");</script>
  • Use the DOM API to add elements:

Code example for the latter:

let e_new = document.createElement("div");
    // Create a new div element for the html document in the context of which this code executes 

document.querySelector('body').append(e_new);
    // Add the new element as the last element on the document, or ...

document.querySelector('#my-parent-element').append(e_new);
    // ... add the new element as the last child an existing element having the `id` attribute of `my-parent-element`.
  • Related