Set - 5

Question 1 :

To set all checkboxes to true using JavaScript? 

Answer :

//select all input tags 
function SelectAll() {
	var checkboxes = document.getElementsByTagName("input");
	for(i=0;i<checkboxes.length;i++) {
		if(checkboxes.item(i).attributes["type"].value == "checkbox") {
			checkboxes.item(i).checked = true;

Question 2 :

How to select an element by id and swapping an image ?

Answer :

<script language="JavaScript" type="text/javascript" >
	function setBeerIcon() {
		var beerIcon = document.getElementById("beerIcon");
		beerIcon.src = "images/"+getSelectValue("beer")+".jpg";
<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>

Question 3 :

What does undefined value mean in javascript?

Answer :

Undefined value means the variable used in the code doesn't exist or is not assigned any value or the property doesn't exist.

Question 4 :

What is the difference between undefined value and null value?

Answer :

(i)Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null
(ii)typeof undefined variable or property returns undefined whereas typeof null value returns object

Question 5 :

What is variable typing in javascript?

Answer :

It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows
i = 10;
i = "string";
This is called variable typing

Question 6 :

Does javascript have the concept level scope?

Answer :

No. JavaScript does not have block level scope, all the variables declared inside a function possess the same level of scope unlike c,c++,java.

Question 7 :

What are undefined and undeclared variables?

Answer :

Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .
Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

Question 8 :

What is === operator ?

Answer :

==== is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.

Question 9 :

How to find the selected radio button immediately using the 'this' variable?

Answer :

	function favAnimal(button) {
		alert('You like '+button.value+'s.');
<input type="radio" name="marsupial" value="kangaroo" onchange="favAnimal(this)">Kangaroo <br />
<input type="radio" name="marsupial" value="Opossum" onchange="favAnimal(this)">Opossum <br />
<input type="radio" name="marsupial" value="Tasmanian Tiger" onchange="favAnimal(this)">Tasmanian Tiger


Question 10 :

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

Answer :

<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) {
		alert("You selected \""+myForm.marsupial[i].value+"\".");
<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 11 :

How to disable an HTML object ?

Answer :

document.getElementById("myObject").disabled = true;

Question 12 :

To write messages to the screen without using "document.write()" ? 

Answer :

Changing the contents of an element is a much better solution. When the method showStatus is invoked it will change the content of the span.

function showStatus(message) {
	var element = document.getElementById("mystatus");
	element.textContent = message; //for Firefox
	element.innerHTML = message; //for IE (why can't we all just get along?)
	return true;
<span id="mystatus">Test. </span>

Question 13 :

How to Add new elements dynamically ?

Answer :

<html xmlns="" xml:lang="en" lang="en">
<script type="text/javascript">
function addNode() {
	var newP = document.createElement("p");
	var textNode = document.createTextNode(" I'm a new text node");
<body onLoad="addNode();" style=" background: url('../images/Sand-1280.jpg'); background-color: yellow;">
    <p id="firstP">firstP<p>

Question 14 :

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

Answer :

<script type="text/javascript">
	function pseudoHitMe() {
<a href="javascript:pseudoHitMe()">hit me</a>

Question 15 :

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

Answer :

<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.