• +91 9971497814
  • info@interviewmaterial.com

Java Script Interview Questions Answers

Question 1 : How to have the status line update when the mouse goes over a link (The support of the status line is sporadic)?

Answer 1 : <a href="javascript.shtml" onmouseover="window.status='Hi There!';return true" onmouseout="window.status='';return true">Look at the Status bar</a> Look at the Status bar as your cursor goes over the link.

Question 2 : How to Add new elements dynamically ?

Answer 2 : <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>t1</title> <script type="text/javascript"> function addNode() { var newP = document.createElement("p"); var textNode = document.createTextNode(" I'm a new text node"); newP.appendChild(textNode); document.getElementById("firstP").appendChild(newP); } </script> </head> <body onload="addNode();" style=" background: url('../images/Sand-1280.jpg'); background-color: yellow;"> <p id="firstP">firstP<p> </body> </html>

Question 3 : How to select an element by id and swapping an image ? ...

Answer 3 : <script language="JavaScript" type="text/javascript" > function setBeerIcon() { var beerIcon = document.getElementById("beerIcon"); beerIcon.src = "images/"+getSelectValue("beer")+".jpg"; } </script> ... <img border="0" src="" id="brandIcon" alt="brand" /> <select name="beer" id="beer" onChange="setButton();setBeerIcon();"> <option value="--Select--">Select beer</option> <option value="heineken">heineken</option> <option value="sol">sol</option> <option value="amstellight">amstellight</option> <option value="coronalight">coronalight</option> <option value="coronaextra">coronaextra</option> <option value=""></option> </select>

Question 4 : how to force a page to go to another page using JavaScript ?

Answer 4 : <script language="JavaScript" type="text/javascript" ><!-- location.href="http://newhost/newpath/newfile.html"; //--></script>

Question 5 : How to find radio button selection when a form is submitted?

Answer 5 : <script type="text/javascript"> function findButton() { var myForm = document.forms.animalForm; var i; for(i=0;i<myForm.marsupial.length; i++) { if(myForm.marsupial[i].checked) { break; } } alert("You selected \""+myForm.marsupial[i].value+"\"."); } </script> <form name="animalForm" action=""> <input type="radio" name="marsupial" value="kangaroo" />Kangaroo <br /><input type="radio" name="marsupial" value="Opossum" />Opossum <br /><input type="radio" name="marsupial" value="Tasmanian Tiger" />Tasmanian Tiger <input type="button" name="GO" value="GO" onclick="findButton()" />

Question 6 : How to have an element invoke a javascript on selection, instead of going to a new URL: ?

Answer 6 : <script type="text/javascript"> function pseudoHitMe() { alert("Ouch!"); } </script> <a href="javascript:pseudoHitMe()">hit me</a>

Question 7 : How tp create Arrays using JavaScript ?

Answer 7 : <script type="text/javascript"> var days = new Array(); days[0] = "Sunday" days[1] = "Monday" days[2] = "Tuesday" days[3] = "Wednesday" days[4] = "Thursday" days[5] = "Friday" days[6] = "Saturday" document.write("first day is "+days[0]) </script> This produces first day is Sunday A more compact way of creating an array is the literal notation: <script type="text/javascript"> var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"]; document.write("first day is "+days[0]) </script> This produces first day is Sunday

Question 8 : How to shift and unshift using JavaScript?

Answer 8 : <script type="text/javascript"> var numbers = ["one", "two", "three", "four"]; numbers.unshift("zero"); document.write(" "+numbers.shift()); document.write(" "+numbers.shift()); document.write(" "+numbers.shift()); </script> This produces zero one two shift, unshift, push, and pop may be used on the same array. Queues are easily implemented using combinations.

Question 9 : How to remove the event listener: ?

Answer 9 : <script type="text/javascript"><!-- document.getElementById("hitme4").removeEventListener("click", hitme4, false); // --> </script> Key Events "onkeydown", "onkeypress", "onkeyup" events are supported both in ie and standards-based browsers. <script type="text/javascript"> function setStatus(name,evt) { evt = (evt) ? evt : ((event) ? event : null); /* ie or standard? */ var charCode = evt.charCode; var status = document.getElementById("keyteststatus"); var text = name +": "+evt.keyCode; status.innerHTML = text; status.textContent = text; } </script> <form action=""> <input type="text" name="keytest" size="1" value="" onkeyup="setStatus('keyup',event)" onkeydown="setStatus('keydown',event)" /> <p id="keyteststatus">status</p> </form>

Question 10 : How to set the focus in an element using Javascript?

Answer 10 : <script> function setFocus() { if(focusElement != null) { document.forms[0].elements["myelementname"].focus(); } } </script>

Java Script Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners