Use of document.getElementById in JavaScript

后端 未结 7 1009
后悔当初
后悔当初 2021-02-06 08:31

Can someone explain what the document.getElementById(\"demo\") line does in the example below?

I understand getElementById gets the id of demo but the id is

7条回答
  •  遥遥无期
    2021-02-06 09:19

    the line

    age=document.getElementById("age").value;
    

    says 'the variable I called 'age' has the value of the element with id 'age'. In this case the input field.

    The line

    voteable=(age<18)?"Too young":"Old enough";
    

    says in a variable I called 'voteable' I store the value following the rule :

    "If age is under 18 then show 'Too young' else show 'Old enough'"

    The last line tell to put the value of 'voteable' in the element with id 'demo' (in this case the 'p' element)

提交回复
热议问题