Posts

Showing posts from April, 2021

maps function

let   array =[    { name : "rajiv" , age : 18 },    { name : "why" , age : 20 },    { name : "wwj" , age : 24 }, ] let   newarray = array . map ( function ( element ){     return   `my name is  ${ element . name }  my age is  ${ element . age } ` }) console . log ( newarray ) console . log ( "helloname" )

local storage and session storage

 they are just a database having key and value  key-value pair  1. local storage        - temporary storage: goes off after turning off the browser         console.log('hello'); localStorage.setItem('name','rajiv') localStorage.setItem('name2','arjun') localStorage.setItem('name3','dhiraj') let name=localStorage.getItem('name') console.log(name) let name3=localStorage.getItem('name3') console.log(name3)      if you want to store non primitive data type like array you need to stringify to JSON while storing and parse while fetching    while storing      let array=['ram','sita',12] console.log(array)   localStorage.setItem('stud',JSON.stringify(array))  while fetching        let str=JSON.parse(localStorage.getItem('stud')) console.log(str)                     same code and syntax for the Session storage but only...

Events and event object

 adding eventlistener       document.getElementById('what').addEventListener( "click",function(){ console.log('you clicked') alert("clicked") }) if you are taking advantage of eventlistener object than you need to pass some argument to function document.getElementById('what').addEventListener( "click",function(e){ console.log('you clicked') let listener=e.offsetX;          listener=e.offsetY; console.log(listener) })  preventdefault event console.log('a') document.getElementById('btn').addEventListener( "click",function(e){ console.log('you clicked') e.preventDefault(); })   other events  console.log('a') /*document.getElementById('what').addEventListener( "dblclick",function(e){ console.log('you clicked') e.preventDefault(); }) */ /*document.getElementById('what').addEventListener( "mousedown",f...

element creation

 want to create and append to the present element? index.html      <!DOCTYPE html> <html> <head> <title> understanding dom</title> </head> <body> <div class="container">  <div class="abc">first div   <ul class="ul">   <li>a</li>   <li>b</li>   <li>c</li>   <li>d</li>   </ul>  </div>  <div class="abc">second div</div>  <div class="abc">third div</div>  <div class="abc">fourth div</div>  </div> </body> <script type="text/javascript" src="javascript.js"></script> </html> js    let element=document.createElement('li') element.className='rajiv' element.id='dahal' let textnode=document.createTextNode('I am a createTextNode') element.appendChild(textnode) console.log(element) le...

traversing in js parent child sibling concept

 you can access child nodes i.e div or query using childnode concept or children concept  childnode includes all text and new line as its children while children concept doesn't include extra lines and spaces as its children  study by assuming a tree structure  index.html               <!DOCTYPE html> <html> <head> <title> understanding dom</title> </head> <body> <div class="container">  <div class="abc">first div   <ul>   <li>a</li>   <li>b</li>   <li>c</li>   <li>d</li>   </ul>  </div>  <div class="abc">second div</div>  <div class="abc">third div</div>  <div class="abc">fourth div</div>  </div> </body> <script type="text/javascript" src="javascript.js"></script> </ht...

selectors : single selector and multi selectors

 single selector:   document.getElementById         let a=document.getElementById('div1'); console.log(a); let element=a.className; console.log(element); a.innerHTML='what is your' a.style.backgroundColor = "red"; a.innerHTML='<b>click here</b>' multiple selector: Select multiple query or class using    1. getElementsByClassName                         let elems=document.getElementsByClassName('div1') Array.from(elems).forEach(function(argument){ console.log(argument); argument.style.backgroundColor = "red"; argument.style.color = "white"; })    

document objects

 console.clear() a=document.links /*console.log(a)*/ Array.from(a).forEach(function (argument) { console.log(argument) // body... }) /*Array.from(a).forEach(function (argument) { if (argument.href.includes('python')) { console.log(argument) } // body... })*/

window objects

 /*a=prompt('enter your name') console.log(a)*/ /*a=confirm('are you sure you want to go back?') console.log(a)*/ /*let a=window.document console.log(a)*/ /*a=innerHeight a=innerWidth console.log(a) */ /*a=scrollX; console.log(a)*/ /*a=scrollY; console.log(a)*/ a=location; console.log(a)

data types

 1.primitive data types    int char number null undefined  2. non primitive    array, js object(just like a dictionary)  let number=12; console.log(number); let array=[1,2,3,4] console.log("hi array type is "+ typeof array) let object={ ram:21, sita:34, gita:41 } console.log(object) console.log(typeof object)  

variables

 var can change const cant be changed let can be changed inside particular function but cant be assign two name inside same function