DEV Community

Cover image for -Dynamic elements, -Data maping Dom, -Attributes
Husniddin6939
Husniddin6939

Posted on

-Dynamic elements, -Data maping Dom, -Attributes

-Dynamic elements - throught this way, we can create elements and move in js like this
1.First of all, we create html tag in js

const div=document.createElement('div')
Enter fullscreen mode Exit fullscreen mode

2.Secondly, we add classList

div.classList.add('p-5','bg-info','m-5')
Enter fullscreen mode Exit fullscreen mode

3.Thirdly, we add new tag in div which we create in js

div.innerHTML='<p>JavaScript</p>'
Enter fullscreen mode Exit fullscreen mode

4.Vital code for access to show in browser, we open new container in html , after that we push our dynamic element in it

container.apend(div)
Enter fullscreen mode Exit fullscreen mode

Image description

5.If we wanna create more cards like so, we should create a new function and also should add loops for controlling its length

function createElement(limit){
    for(let i=0; i<limit; i++){
        const div=document.createElement('div');
        div.classList.add('p-5', 'bg-info','m-5');
        div.innerHTML='<p>JavaScript</p>';
        container.prepend(div)
    }

}

createElement(3)
Enter fullscreen mode Exit fullscreen mode

Image description

                                  After  Before
Enter fullscreen mode Exit fullscreen mode

This keys put our dynamic elements right places with html tags.
For instace we have once html tag and anotherone is dynamic element we create, now let's contain our cards right places with after,before kays

"use-strict";
const button=$('.btn-danger')

const btnjs=document.createElement('button');
      btnjs.classList.add('btn-success','btn','m-5');
      btnjs.textContent='Js button';


      button.after(btnjs);
Enter fullscreen mode Exit fullscreen mode

Image description

in this case, we put js dynamic button next of html buuton throught After js key.

                                  Before
Enter fullscreen mode Exit fullscreen mode
"use-strict";
const button=$('.btn-danger')

const btnjs=document.createElement('button');
      btnjs.classList.add('btn-success','btn','m-3');
      btnjs.textContent='Js button';


      button.before(btnjs);
Enter fullscreen mode Exit fullscreen mode

Image description

                               Attributes
Enter fullscreen mode Exit fullscreen mode

Attribute - Example for attribute, in this code we have seen such kind of attributes like id,type,class..etc

<button id="btn-danger" type="button" class="btn-danger btn m-3">html button</button>
Enter fullscreen mode Exit fullscreen mode

How to get attribute in js ? Simple !!!! Like this

console.log(button.getAttribute('id'));
Enter fullscreen mode Exit fullscreen mode

Image description

Another key of attribute is setting new attribute or change their value if there is such element

button.setAttribute('title','button')
console.log(button);
Enter fullscreen mode Exit fullscreen mode

Image description

If we try to set again this exsist Attribute in it , it will change its value

button.setAttribute('title','button')
console.log(button);

button.setAttribute('title','form')
console.log(button);
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)