jquery - My javascript is not working for dom traversing -
i'm learning javascript , jquery. discovered dom traversing.but when i'm writing code it,it not responding. code :
<!doctype html> <head> <title>test</title> </head> <body> <script> $('div').onclick = function pi() { var p = document.getelementsbyclassname('.p'); p.value("hello guys"); }; </script> <div style="background-color:blue; height:1000px; width:100%px;"> </div> <div style="height: 600px; background-color: aqua;width: auto;"> <p style="color:black; font-size: 40px;" class="p">d </p> </div> </body> </html>
let's go through problems:
1) haven't included jquery anywhere, can't use it. if want use it, need add script tag (between <head> tags given current use) linking - like:
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-it6q9imjyuqimwnd9ldybustiq/8puow33aoqmvfpqi=" crossorigin="anonymous"></script> 2) that's how jquery's onclick (which click) works. click function accepts function. function pass click executed when user clicks on target. code overwriting jquery's click, should instead be:
$('div').click(function pi() { // code }) 3) it's not forbidden or anything, since using jquery, doesn't make ton of sense fall native dom fetching apis getelementsbyclassname. recommend instead use $('.p'):
$('div').click(function pi() { // it's prefix jquery collection variables $ // makes obvious in them var $p = $('.p') }) side note can use $('p') , query based on tag name, avoids assigning redundant class name
4) .value() neither jquery method or native method. jquery has val() method, setting value property of input elements select, input, etc. wanting change text of p tag. jquery $('p').text('new text'). please note calling change text all p elements, or class="p" elements in case:
$('div').click(function pi() { var $p = $('.p') $p.text('hello guys') }); hope helps on way
Comments
Post a Comment