Author Topic: Java Script 10 Dan 21 DOM SELECTION elemenata  (Read 2412 times)

0 Members and 1 Guest are viewing this topic.

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 DOM SELECTION elemenata
« on: 26 October 2025, 23:13:33 »
Code: (HTML) [Select]
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <!-- <script src="main.js" defer></script> -->
    <!-- <script src="traversing.js" defer></script> -->
    <!-- <script src="variousSelectors.js" defer></script> -->
    <!-- <script src="attributesModification.js" defer></script> -->
    <!-- <script src="styleModification.js" defer></script> -->
    <!-- <script src="addingRemovingMoving.js" defer></script> -->
    <script src="event.js" defer></script>
  </head>
  <body>
    <header>
      <h1>Ucimo DOM</h1>
      <nav>
        <ul>
          <li><a href="#about">About</a></li>
          <li><a href="#projects">Projects</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    </header>
    <main>
      <section id="about" class="sec aboutSec">
        <h2>About</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero nemo
          amet reiciendis autem. Hic reiciendis earum eveniet ullam, nemo sit
          sequi suscipit ex placeat commodi praesentium delectus consequuntur
          impedit perspiciatis.
        </p>
        <ul>
          <li>HTML</li>
          <li>CSS</li>
          <li>JS</li>
        </ul>
      </section>

      <section id="projects" class="sec projectSec">
        <h2>Projects</h2>
        <article class="default-article">
          <h2>Project 1</h2>
          <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam
            doloribus tempore eius cupiditate, atque quos nostrum eligendi unde
            saepe, itaque commodi hic iste laboriosam est provident ut
            voluptates, voluptatem necessitatibus!
          </p>
        </article>

        <article class="default-article">
          <h2>Project 2</h2>
          <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.
          </p>
        </article>
      </section>
      <button class="addProject">Add project</button>
      <button class="deleteProject">Delete project</button>

      <section id="contact" class="sec constactSec">
        <h2>Contact</h2>
        <form>
          <label for="name">Name</label><br />
          <input type="text" id="name" name="ime" />
          <br /><br />
          <label for="">Email</label><br />
          <input type="email" id="email" name="email" />
          <br /><br />
          <label for="message">Message</label><br />
          <textarea name="message" id="message" name="message"></textarea>
          <br /><br />
          <button type="submit">Submit</button>
        </form>
      </section>
    </main>
    <footer>
      <p>DOM manipulation</p>
    </footer>
  </body>
</html>





Code: (SELECTION JS) [Select]
//DOM -document Object Model
//DOM API- metode i propertiji koji nam omogucavaju da radimo sa DOM-om

console.log(document);

//"Dohvatanje " elemenata DOM-a

I nacin
const sectionAbout = document.getElementById("about");
console.log(sectionAbout);
------

//null vrednost dobijamo kada ne mozemo da pronadjemo element
const unknownElement = document.getElementById("xxx");
console.log(unknownElement); //null
------

// HTML Collection je nesto slicno nizovima
const sectionProjects = document.getElementsByClassName("projectSec")[0];
console.log(sectionProjects); //HTMLCollection
------

//II primer Ako u html dodamo 2 klase koje imaju pre imena sec (sec ProjectSec), dobijamo u HTML Collection sve koje imaju sec i [0] za prvi, [1] za drugi, ili bez ovog u zagradama za sve
const sections = document.getElementsByClassName("sec");
console.log(sections);
------

// HTML collection za header element
const headerElement = document.getElementsByTagName("header"); //HTMLCollection
console.log(headerElement);
------

// Dobija se NODE LIST kada logujemo prazno, mora da se stavi [0] da bi se uhvatio element
const inputEl = document.getElementsByName("email");
console.log(inputEl); // NodeList

//Da li postoji vise elemenata
if (inputEl.length > 0) {
}

if (sectionAbout !== null) { // da li je section postoji
}
-------

// Da bismo pristupili DOM-u uvek mora = document.
// daje nam h1 element
const h1Element = document.querySelector("header > h1");
console.log(h1Element);
------

// header i h1 su sestrinski elementi ispod body
const h1Element1 = document.querySelector("body > header > h1");
console.log(h1Element1);
------

// dobijaju se NODE list svih sekcija section i koliko elemenata trenutno ima
const allSections = document.querySelectorAll("main > section"); //NodeList
console.log(allSections);

// dobija se sekcija projects jer je poslednja sekcija
console.log(allSections[allSections.length - 1]);



//DOM -document Object Model
//DOM API- metode i propertiji koji nam omogucavaju da radimo sa DOM-om

console.log(document);

//"Dohvatanje " elemenata DOM-a

I nacin
const sectionAbout = document.getElementById("about");
console.log(sectionAbout);
------

//null vrednost dobijamo kada ne mozemo da pronadjemo element
const unknownElement = document.getElementById("xxx");
console.log(unknownElement); //null
------

// HTML Collection je nesto slicno nizovima
const sectionProjects = document.getElementsByClassName("projectSec")[0];
console.log(sectionProjects); //HTMLCollection
------

//II primer Ako u html dodamo 2 klase koje imaju pre imena sec (sec ProjectSec), dobijamo u HTML Collection sve koje imaju sec i [0] za prvi, [1] za drugi, ili bez ovog u zagradama za sve
const sections = document.getElementsByClassName("sec");
console.log(sections);
------

// HTML collection za header element
const headerElement = document.getElementsByTagName("header"); //HTMLCollection
console.log(headerElement);
------

// Dobija se NODE LIST kada logujemo prazno, mora da se stavi [0] da bi se uhvatio element
const inputEl = document.getElementsByName("email");
console.log(inputEl); // NodeList

//Da li postoji vise elemenata
if (inputEl.length > 0) {
}

if (sectionAbout !== null) { // da li je section postoji
}
-------

// Da bismo pristupili DOM-u uvek mora = document.
// daje nam h1 element
const h1Element = document.querySelector("header > h1");
console.log(h1Element);
------

// header i h1 su sestrinski elementi ispod body
const h1Element1 = document.querySelector("body > header > h1");
console.log(h1Element1);
------

// dobijaju se NODE list svih sekcija section i koliko elemenata trenutno ima
const allSections = document.querySelectorAll("main > section"); //NodeList
console.log(allSections);

// dobija se sekcija projects jer je poslednja sekcija
console.log(allSections[allSections.length - 1]);
« Last Edit: 27 October 2025, 00:54:56 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 TRANVERSING kretanje kroz DOM strukturu
« Reply #1 on: 26 October 2025, 23:33:32 »
TRANVERSING kretanje kroz DOM strukturu

Code: (TRANVERSING JS) [Select]
//kretanje kroz DOM strukturu

// VRACA NAM main sekciju
const mainElement = document.getElementsByTagName("main")[0];
console.log(mainElement);
-----

// dobicemo kao rezultat body element
const parentOfMainElement = mainElement.parentElement;
console.log(parentOfMainElement);
-----

// dobicemo HTML selection svih children elemenata, 2 sekcije children-a main elementa
const childrenOfMainElement = mainElement.children;
console.log(childrenOfMainElement);
-----

// dobicemo sekciju ID = about
const firstChildOfMainElement = mainElement.firstElementChild;
console.log(firstChildOfMainElement);
-----

//II primer
// dobijamo text/cvor na DOM - ovo ne koristimo
// const firstMainChild = mainElement.firstChild;
// console.log(firstMainChild);
-----

// dobijamo section projects
const lastChildOfMainElement = mainElement.lastElementChild;
console.log(lastChildOfMainElement);
-----

const ulElement = document.querySelector("ul");
console.log(ulElement); // dobijamo nasu listu
console.log(ulElement.children); // dobijamo HTML Collection sve 3 liste
console.log(ulElement.childNodes);  // dohvata sve u cvoru (text, li, text, li, text, li, text) i pokazuje duzinu child elemenata za ul





//kretanje kroz DOM strukturu

// VRACA NAM main sekciju
const mainElement = document.getElementsByTagName("main")[0];
console.log(mainElement);
-----

// dobicemo kao rezultat body element
const parentOfMainElement = mainElement.parentElement;
console.log(parentOfMainElement);
-----

// dobicemo HTML selection svih children elemenata, 2 sekcije children-a main elementa
const childrenOfMainElement = mainElement.children;
console.log(childrenOfMainElement);
-----

// dobicemo sekciju ID = about
const firstChildOfMainElement = mainElement.firstElementChild;
console.log(firstChildOfMainElement);
-----

//II primer
// dobijamo text/cvor na DOM - ovo ne koristimo
// const firstMainChild = mainElement.firstChild;
// console.log(firstMainChild);
-----

// dobijamo section projects
const lastChildOfMainElement = mainElement.lastElementChild;
console.log(lastChildOfMainElement);
-----

const ulElement = document.querySelector("ul");
console.log(ulElement); // dobijamo nasu listu
console.log(ulElement.children); // dobijamo HTML Collection sve 3 liste
console.log(ulElement.childNodes);  // dohvata sve u cvoru (text, li, text, li, text, li, text) i pokazuje duzinu child elemenata za ul
« Last Edit: 27 October 2025, 00:53:52 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 DOM VARIOUS SELECTORS
« Reply #2 on: 27 October 2025, 00:18:40 »
VARIOUS SELECTORS

Code: (VARIOUS SELECTORS JS) [Select]
Mozemo doci u situaciju da mozemo samo neke od ovih nacina da primenimo da nadjemo odredjeni element

// ("#PREKO id")
const sectionProjects = document.querySelector("#projects");
console.log(sectionProjects);   // dobijamo ceo section projects
-----

// dobija se isto kao i iznad
const sectionProjectsWithID = document.getElementById("projects");
console.log(sectionProjectsWithID);
-----

// dobijamo prvi projekat/article
const firstProject = sectionProjects.querySelector("article");
console.log(firstProject);
-----

// dobijamo HTML kolekciju (bez [0]), a kad stavimo 0 dobijamo article
const firstProjectFirstWay =
  document.getElementsByClassName("default-article")[0];
console.log(firstProjectFirstWay);
-----

// dobija se NODE LIST, i moramo da dodamo [0] da bi dobili article
const firstProjectSecondWay = document.querySelectorAll(
  "main > section#projects > article"
)[0];
console.log(firstProjectSecondWay);
-----

// dobijamo article
const firstProjectThirdWay = document.querySelector(
  "#projects > article:nth-of-type(1)"
);
console.log(firstProjectThirdWay);
-----

// dobijamo NODE list, i kad stavimo [0] dobijamo article
const firstProjectFourthWay = sectionProjects.querySelectorAll("article")[0];
console.log(firstProjectFourthWay);
-----


const firstProjectFifthWay =
  sectionProjects.firstElementChild; // dobijamo H2
 sectionProjects.firstElementChild.nextElementSibling; // kad dodamo metodu dobijamo, element koji je u istom nivou ce se prikazati - article

console.log(firstProjectFifthWay);
-----

// dobijamo article
const firstProjectSixtWay = sectionProjects.children[1];
console.log(firstProjectSixtWay);
-----

const firstProjectSeventWay =
  document.getElementsByTagName("h2").parentElement;  // dobijamo kolekciju svih H2 elemenata
  document.getElementsByTagName("h2")[2].parentElement;  // kada dodamo [2] dobijamo treci, odnosno article
console.log(firstProjectSeventWay);



Mozemo doci u situaciju da mozemo samo neke od ovih nacina da primenimo da nadjemo odredjeni element

// ("#PREKO id")
const sectionProjects = document.querySelector("#projects");
console.log(sectionProjects);   // dobijamo ceo section projects
-----

// dobija se isto kao i iznad
const sectionProjectsWithID = document.getElementById("projects");
console.log(sectionProjectsWithID);
-----

// dobijamo prvi projekat/article
const firstProject = sectionProjects.querySelector("article");
console.log(firstProject);
-----

// dobijamo HTML kolekciju (bez [0]), a kad stavimo 0 dobijamo article
const firstProjectFirstWay =
  document.getElementsByClassName("default-article")[0];
console.log(firstProjectFirstWay);
-----

// dobija se NODE LIST, i moramo da dodamo [0] da bi dobili article
const firstProjectSecondWay = document.querySelectorAll(
  "main > section#projects > article"
)[0];
console.log(firstProjectSecondWay);
-----

// dobijamo article
const firstProjectThirdWay = document.querySelector(
  "#projects > article:nth-of-type(1)"
);
console.log(firstProjectThirdWay);
-----

// dobijamo NODE list, i kad stavimo [0] dobijamo article
const firstProjectFourthWay = sectionProjects.querySelectorAll("article")[0];
console.log(firstProjectFourthWay);
-----


const firstProjectFifthWay =
  sectionProjects.firstElementChild; // dobijamo H2
 sectionProjects.firstElementChild.nextElementSibling; // kad dodamo metodu dobijamo, element koji je u istom nivou ce se prikazati - article

console.log(firstProjectFifthWay);
-----

// dobijamo article
const firstProjectSixtWay = sectionProjects.children[1];
console.log(firstProjectSixtWay);
-----

const firstProjectSeventWay =
  document.getElementsByTagName("h2").parentElement;  // dobijamo kolekciju svih H2 elemenata
  document.getElementsByTagName("h2")[2].parentElement;  // kada dodamo [2] dobijamo treci, odnosno article
console.log(firstProjectSeventWay);

« Last Edit: 27 October 2025, 02:29:11 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 DOM ATTRIBUTES MODIFICATION
« Reply #3 on: 27 October 2025, 00:20:35 »
ATTRIBUTES MODIFICATION

Code: (ATTRIBUTES MODIFICATION JS) [Select]
// HTML collection 0, 1, 2.  // kada dodamo [0] dohvatili smo #about
const firstLink = document.getElementsByTagName("a")[0];
console.log(firstLink);
-----

// dobija se #about
const valueOfHrefForFirstLink = firstLink.getAttribute("href");
console.log(valueOfHrefForFirstLink); //relativna putanja #about
-----

//II nacin
console.log(firstLink.href); //apsolutna putanja - http://127.0.0.1:5500/DOM/index.html#about
-----

// menjamo i dodajemo atribute
if (firstLink.hasAttribute("href")) {
  //   firstLink.setAttribute("href", "http://google.com"); -izmena atributa
  //   firstLink.href = "http://youtube.com"; -izmena atributa
  //   firstLink.removeAttribute("href"); -uklanjanje atributa
  //   firstLink.setAttribute("id", "linkAbout"); -dodavanje atributa
}
-----

//II primer
//
const inputEmail = document.getElementById("email");
console.log(inputEmail);

inputEmail.setAttribute("placeholder", "example@gmail.com");
inputEmail.setAttribute("value", "");
inputEmail.value = "maja@gmail.com";
-----

// dobijamo ul listu
const ulList = document.querySelector("nav > ul");
console.log(ulList);

//metoda .textContent
console.log(ulList.textContent);   // vraca nam sadrzaje UL liste/naslove - about, projects, contact i moze se ekspand

//metoda .innerText
console.log(ulList.innerText);  // vraca nam samo about, projects, contact bez icega

//metoda .innerHTML
console.log(ulList.innerHTML);  // omogucava da vidimo ceo HTML elementa, sve sto je pod li tag
-----

firstLink.innerText = "Google";  //  vrednost ABOUT se menja u google
-----

// menjamo imena kompletne liste navigacije, znaci umesto about, projects, contact ima ovo ispod
ulList.innerHTML = `
<li>HTML</li>
<li>CSS</li>
<li>JS</li>`;




// HTML collection 0, 1, 2.  // kada dodamo [0] dohvatili smo #about
const firstLink = document.getElementsByTagName("a")[0];
console.log(firstLink);
-----

// dobija se #about
const valueOfHrefForFirstLink = firstLink.getAttribute("href");
console.log(valueOfHrefForFirstLink); //relativna putanja #about
-----

//II nacin
console.log(firstLink.href); //apsolutna putanja - http://127.0.0.1:5500/DOM/index.html#about
-----

// menjamo i dodajemo atribute
if (firstLink.hasAttribute("href")) {
  //   firstLink.setAttribute("href", "http://google.com"); -izmena atributa
  //   firstLink.href = "http://youtube.com"; -izmena atributa
  //   firstLink.removeAttribute("href"); -uklanjanje atributa
  //   firstLink.setAttribute("id", "linkAbout"); -dodavanje atributa
}
-----

//II primer
//
const inputEmail = document.getElementById("email");
console.log(inputEmail);

inputEmail.setAttribute("placeholder", "example@gmail.com");
inputEmail.setAttribute("value", "");
inputEmail.value = "maja@gmail.com";
-----

// dobijamo ul listu
const ulList = document.querySelector("nav > ul");
console.log(ulList);

//metoda .textContent
console.log(ulList.textContent);   // vraca nam sadrzaje UL liste/naslove - about, projects, contact i moze se ekspand

//metoda .innerText
console.log(ulList.innerText);  // vraca nam samo about, projects, contact bez icega

//metoda .innerHTML
console.log(ulList.innerHTML);  // omogucava da vidimo ceo HTML elementa, sve sto je pod li tag
-----

firstLink.innerText = "Google";  //  vrednost ABOUT se menja u google
-----

// menjamo imena kompletne liste navigacije, znaci umesto about, projects, contact ima ovo ispod
ulList.innerHTML = `
<li>HTML</li>
<li>CSS</li>
<li>JS</li>`;


« Last Edit: 27 October 2025, 02:29:01 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 DOM Style Modification
« Reply #4 on: 27 October 2025, 00:52:54 »
STYLE MODIFICATION



Code: (STYLE MODIFICATION JS) [Select]

const headerEl = document.getElementsByTagName("header")[0];
console.log(headerEl);

//primena inline stila IMA PREDNOST U ODNOSU NA KLASE
// headerEl.style.backgroundColor = "lightblue";

//.add(), remove() metode
// headerEl.classList.add("testClass");
// headerEl.classList.remove("testClass");
// headerEl.classList.add("headerClass");

console.log(headerEl.classList);  // dobijamo DOMtokenCLASSlist

//.toggle() metoda

headerEl.classList.toggle("testClass");
//ukoliko postoji klasa, metoda toggle je uklanja, ukoliko ne postoji klasa, metoda je postavlja







Code: (CSS STYLE MODIFICATION) [Select]
#projects {
  background-color: rgb(253, 183, 183);
}

#projects h2 {
  color: blue;
}

.testClass {
  background-color: burlywood;
  font-size: 16px;
}

button {
  background-color: rgb(91, 53, 53);
  color: white;
  padding: 0.5rem;
  border: none;
}
.changeColor {
  background-color: brown;
}






const headerEl = document.getElementsByTagName("header")[0];
console.log(headerEl);

//primena inline stila IMA PREDNOST U ODNOSU NA KLASE
// headerEl.style.backgroundColor = "lightblue";

//.add(), remove() metode
// headerEl.classList.add("testClass");
// headerEl.classList.remove("testClass");
// headerEl.classList.add("headerClass");

console.log(headerEl.classList);  // dobijamo DOMtokenCLASSlist

//.toggle() metoda

headerEl.classList.toggle("testClass");
//ukoliko postoji klasa, metoda toggle je uklanja, ukoliko ne postoji klasa, metoda je postavlja








#projects {
  background-color: rgb(253, 183, 183);
}

#projects h2 {
  color: blue;
}

.testClass {
  background-color: burlywood;
  font-size: 16px;
}

button {
  background-color: rgb(91, 53, 53);
  color: white;
  padding: 0.5rem;
  border: none;
}
.changeColor {
  background-color: brown;
}

« Last Edit: 27 October 2025, 02:28:52 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 10 Dan 21 DOM ADDING REMOVING MOVING elements
« Reply #5 on: 27 October 2025, 02:28:40 »
ADDING REMOVING MOVING ELEMENTS

Code: [Select]

//za kreiranje elemenata koristimo metodu .createElement
const newParagraph = document.createElement("p");
console.log(newParagraph);

newParagraph.textContent = "Manipulacija DOM-om";
newParagraph.style.fontSize = "25px";
newParagraph.style.color = "purple";

const aboutSection = document.getElementById("about");

//metoda appendChild dodaje element na poslednje mesto
aboutSection.appendChild(newParagraph);

const sectionProjects = document.getElementById("projects");
function createProject() {
  // kreiranje artikla
  const newArticle = document.createElement("article");
  newArticle.classList.add("default-article");

  //kreiranje h2
  const headingElement = document.createElement("h2");
  headingElement.textContent = "Project 3";

  //kreiranje p
  const paragraphEl = document.createElement("p");
  paragraphEl.textContent = `Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.`;

  newArticle.appendChild(headingElement);
  newArticle.appendChild(paragraphEl);

  sectionProjects.appendChild(newArticle);
}

createProject();

//metoda .remove()
function removeProject() {
  sectionProjects.lastElementChild.remove();
}


// UKLANJA 1 PO 1 child element u okviru section dok ne ukloni i section i onda daje void
// removeProject();
// removeProject();
// removeProject();
// removeProject();
// removeProject();


//II nacin kreiranja elemenata

function createElementSecondWay() {
  sectionProjects.innerHTML += `  // obratiti paznju na +=, mozemo mnogo da zeznemo
    <article class="default-article">
        <h2>Project 4</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.</p>
    </article>`;
}

createElementSecondWay();









//za kreiranje elemenata koristimo metodu .createElement
const newParagraph = document.createElement("p");
console.log(newParagraph);

newParagraph.textContent = "Manipulacija DOM-om";
newParagraph.style.fontSize = "25px";
newParagraph.style.color = "purple";

const aboutSection = document.getElementById("about");

//metoda appendChild dodaje element na poslednje mesto
aboutSection.appendChild(newParagraph);

const sectionProjects = document.getElementById("projects");
function createProject() {
  // kreiranje artikla
  const newArticle = document.createElement("article");
  newArticle.classList.add("default-article");

  //kreiranje h2
  const headingElement = document.createElement("h2");
  headingElement.textContent = "Project 3";

  //kreiranje p
  const paragraphEl = document.createElement("p");
  paragraphEl.textContent = `Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.`;

  newArticle.appendChild(headingElement);
  newArticle.appendChild(paragraphEl);

  sectionProjects.appendChild(newArticle);
}

createProject();

//metoda .remove()
function removeProject() {
  sectionProjects.lastElementChild.remove();
}


// UKLANJA 1 PO 1 child element u okviru section dok ne ukloni i section i onda daje void
// removeProject();
// removeProject();
// removeProject();
// removeProject();
// removeProject();


//II nacin kreiranja elemenata

function createElementSecondWay() {
  sectionProjects.innerHTML += `  // obratiti paznju na +=, mozemo mnogo da zeznemo
    <article class="default-article">
        <h2>Project 4</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.</p>
    </article>`;
}

createElementSecondWay();

« Last Edit: 27 October 2025, 02:50:39 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Re: Java Script 10 Dan 21 DOM EVENT
« Reply #6 on: 27 October 2025, 04:32:57 »
EVENT

Je funkcija koja osluskuje neki dogadjaj u nasem pretrazivacu na koji reaguje, od klika pa sve ostalo

Code: (EVENT JS) [Select]

const addProjectButton = document.getElementsByClassName("addProject")[0];
console.log(addProjectButton);
const sectionProjects = document.getElementById("projects");

const deleteButton = document.getElementsByClassName("deleteProject")[0];

const nameInput = document.getElementById("name");

// Prvi deo je klik, drugi je funkcija koja ce se izvrsiti kada se event desi
// umesto click moze "mouseover", mousedown, keyup, keydown, change, submit, clickdown, loadevent
// addProjectButton.addEventListener("click", function (event) {
//   //   console.log("Button is clicked");

//   console.log(event);
// });

function addNewProject(numOfProject) {
  sectionProjects.innerHTML += `
  <article class="default-article">
          <h2>Project ${numOfProject}</h2>
          <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.
          </p>
        </article>
    `;
}

// const newProjectNum = document.getElementsByClassName("default-article").length;
// console.log(newProjectNum);

// addNewProject(newProjectNum + 1);
// addNewProject(newProjectNum + 1);

addProjectButton.addEventListener("click", function (event) {
  const newProjectNum =
    document.getElementsByClassName("default-article").length;
  addNewProject(newProjectNum + 1);
});

deleteButton.addEventListener("click", function () {
  sectionProjects.lastElementChild.remove();
});

const callback = function (event) {
  //   console.log(event);
  event.preventDefault();
  console.log(event.key);
};

nameInput.addEventListener("keyup", callback);

//event delegation
const ulElement = document.getElementsByTagName("ul")[0];
ulElement.addEventListener("click", function (e) {
  console.log(e);
  //   console.log("Klik se desio na ul elementu");

  //   if (e.target.tagName === "A") {
  //     console.log(`Kliknuto je na link ${e.target.textContent}`);
  //   }

  ulElement.style.backgroundColor = "lightblue";
});

const anchor = document.querySelector("a");
console.log(anchor);

anchor.addEventListener("click", function (e) {
  anchor.style.color = "red";
  anchor.style.fontSize = "30px";
});

sectionProjects.addEventListener("click", function (e) {
  console.log(e.target);

  console.log(`this ukazuje na:`, this);

  if (e.target.classList.contains("default-article")) {
    e.target.classList.toggle("changeColor");
  }
});

//kljucna rec this







const addProjectButton = document.getElementsByClassName("addProject")[0];
console.log(addProjectButton);
const sectionProjects = document.getElementById("projects");

const deleteButton = document.getElementsByClassName("deleteProject")[0];

const nameInput = document.getElementById("name");

// Prvi deo je klik, drugi je funkcija koja ce se izvrsiti kada se event desi
// umesto click moze "mouseover", mousedown, keyup, keydown, change, submit, clickdown, loadevent
// addProjectButton.addEventListener("click", function (event) {
//   //   console.log("Button is clicked");

//   console.log(event);
// });

function addNewProject(numOfProject) {
  sectionProjects.innerHTML += `
  <article class="default-article">
          <h2>Project ${numOfProject}</h2>
          <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa
            tenetur sint ab nobis nesciunt quae neque, necessitatibus omnis quod
            animi. Earum molestias eos mollitia beatae fuga dolorem, totam
            voluptatem? Deserunt.
          </p>
        </article>
    `;
}

// const newProjectNum = document.getElementsByClassName("default-article").length;
// console.log(newProjectNum);

// addNewProject(newProjectNum + 1);
// addNewProject(newProjectNum + 1);

addProjectButton.addEventListener("click", function (event) {
  const newProjectNum =
    document.getElementsByClassName("default-article").length;
  addNewProject(newProjectNum + 1);
});

deleteButton.addEventListener("click", function () {
  sectionProjects.lastElementChild.remove();
});

const callback = function (event) {
  //   console.log(event);
  event.preventDefault();
  console.log(event.key);
};

nameInput.addEventListener("keyup", callback);

//event delegation
const ulElement = document.getElementsByTagName("ul")[0];
ulElement.addEventListener("click", function (e) {
  console.log(e);
  //   console.log("Klik se desio na ul elementu");

  //   if (e.target.tagName === "A") {
  //     console.log(`Kliknuto je na link ${e.target.textContent}`);
  //   }

  ulElement.style.backgroundColor = "lightblue";
});

const anchor = document.querySelector("a");
console.log(anchor);

anchor.addEventListener("click", function (e) {
  anchor.style.color = "red";
  anchor.style.fontSize = "30px";
});

sectionProjects.addEventListener("click", function (e) {
  console.log(e.target);

  console.log(`this ukazuje na:`, this);

  if (e.target.classList.contains("default-article")) {
    e.target.classList.toggle("changeColor");
  }
});

//kljucna rec this

« Last Edit: 27 October 2025, 04:35:08 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Tags:
 


SimplePortal 2.3.7 © 2008-2026, SimplePortal