Dom javascript

bekmuhammaddev

BekmuhammadDev

Posted on June 26, 2024

Dom javascript

creat elemts:

const div = document.createElement("div"); //Element yaratish ya'ni shu xolatda <div></div>
const h1 = document.createElement("h1"); //Element yaratish ya'ni  shu xolatda <h1></h1>
Enter fullscreen mode Exit fullscreen mode

addClassList:

div.classList.add('p-5', 'bg-success'); // class qo'shish ya'ni shu xolatda 

div.innerHTML = "<h1>Hello world!</h1>";//Htmlda chiqarish


Enter fullscreen mode Exit fullscreen mode

AllCode:

const div = document.createElement("div"); //Element yaratish ya'ni shu xolatda <div></div>
const h1 = document.createElement("h1"); //Element yaratish ya'ni  shu xolatda <h1></h1>


div.classList.add('p-5', 'bg-success'); // class qo'shish ya'ni shu xolatda 
div.innerHTML = "<h1>Hello world!</h1>";//Htmlda chiqarish


console.log(h1);
console.log(div);
Enter fullscreen mode Exit fullscreen mode

CreatElement Function:

function CreateElemt() {
    const div = document.createElement("div");
    div.classList.add('p-5', 'bg-success');
    div.innerHTML = "<h1>Hello world!</h1>";
    cotainer.div.append(div)


}

CreateElemt()   //call qilish orqali nusxalab ya'ni ko'paytirish mumkin!
Enter fullscreen mode Exit fullscreen mode

CreatElement Function For:

function CreateElemt(limit) {
   for (let i = 0; i < i.limit; index++) {
    const div = document.createElement("div");
    div.classList.add('p-5', 'bg-success');
    div.innerHTML = "<h1>Hello world!</h1>";
    cotainer.div.append(div)

   }


}

CreateElemt(10)   //funksiyani call qilish va unga necha marotaba ko'paytirish keragligini yozish mumkin
Enter fullscreen mode Exit fullscreen mode

Attribute:
getAttribute:
setAttribute
*getAttribute Metodi
*

Bu metod DOM elementining berilgan atribut qiymatini qaytaradi.

element.getAttribute(attribut_nomi)

Enter fullscreen mode Exit fullscreen mode

Masalan:

let elem = document.getElementById('myElement');
let value = elem.getAttribute('class');
console.log(value);  // Atributning qiymati, masalan: "myClass"

Enter fullscreen mode Exit fullscreen mode

setAttribute Metodi

Bu metod DOM elementiga yangi atribut qo'shadi yoki mavjud atributning qiymatini o'zgartiradi.

element.setAttribute(attribut_nomi, yangi_ qiymat)

Enter fullscreen mode Exit fullscreen mode
let elem = document.getElementById('myElement');
elem.setAttribute('class', 'newClass');

Enter fullscreen mode Exit fullscreen mode

hasAttribute
JavaScriptda DOM elementining atributi mavjudligini tekshirish uchun hasAttribute metodidan foydalaniladi. Bu metod berilgan atribut elementda mavjud bo'lsa, true qiymatini, aks holda false qiymatini qaytaradi.

element.hasAttribute(attribut_nomi)

Enter fullscreen mode Exit fullscreen mode

hasAttribute metodi yordamida har bir atributning mavjudligi tekshiriladi va natijalar konsolga chiqariladi.

getAttribute va setAttribute metodlari DOM manipulyatsiyasi uchun juda foydali hisoblanadi.

💖 💪 🙅 🚩
bekmuhammaddev
BekmuhammadDev

Posted on June 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Dom javascript
dom Dom javascript

June 26, 2024