JavaScript Interview Questions and Answers (294) - Page 7

How to stop the Timer in Javascript?

The clearInterval() static method of window class is used to stop timer of the function specified in the setInterval() method.

For Example:-
<script type="text/javascript">

var str = setInterval(function(){start_timer()},3000);

function start_timer()
{
alert("Hello");
}

function stop_timer()
{
window.clearInterval(str);
}
</script>

How to Disable any controls using Javascript?

With the help of disabled property,we can Disable any controls in Javascript.

In Dot net,we have Enabled property,but in Javascript it's just opposite.

For Example:-

<script type="text/javascript">

function disable_controls()
{
document.getElementById('<%=btn_read_cookies.ClientID%>').disabled = true;
}
</script>


<asp:Button ID="Button1" runat="server" Text="Click" OnClientClick="disable_controls(); return false;" />

How to get Dropdown list selected Text through Javascript?

var ddl = document.getElementById('<%=dropdownlist1.ClientID%>');

var text = ddl.options[ddl.selectedIndex].text;
alert(text);

How many ways to assign values to ViewState?

NOTE: This is objective type question, Please click question title for correct answer.
How does a For Loop start in Javascript?

NOTE: This is objective type question, Please click question title for correct answer.
How to get Dropdown list selected Value through Javascript?

var ddl = document.getElementById('<%=dropdownlist1.ClientID%>');


var value = ddl.options[ddl.selectedIndex].value;

alert(value);

How to check whether a text box is read-only or not?

var is_readonly = document.getElementById('<%=txt_description.ClientID%>').readOnly;


<asp:TextBox ID="txt_description" runat="server" TextMode="MultiLine" ReadOnly="true"></asp:TextBox>


Output will be True.
How to set Textbox control to Readonly in Javascript?

Set the readonly property of textbox control to true/false.

For Example:-

document.getElementById("txt_description").readOnly = true;

How to check whether Dropdownlist has any items or not in Javascript?

With the help of options.length property of Dropdownlist control,we can check.

var ddl = document.getElementById('<%=ddl_name_list.ClientID%>');

alert(ddl.options.length);

How to check whether CheckBox is checked or not?

With the help of checked property of checkbox,we can check whether it is checked or not.
It returns true or false.

For Example:-

alert(document.getElementById('chk_employee_active').checked);

<asp:CheckBox ID="chk_employee_active" runat="server" />

How to check whether Textbox object is initialized or not in Javascript?

We can check textbox object as,

var txt_emp_salary = document.getElementById("<%=txt_employee_salary.ClientID%>");

if(txt_emp_salary !=null)
alert('Object is initialized');
else
alert('Object is not initialized');
How to check Textbox contains any values or not?

We can check with 2 ways:-

var txt_emp_salary = document.getElementById("<%=txt_employee_salary.ClientID%>");


if(txt_emp_salary !=null)
{
//1st way
if(txt_emp_salary.value!= '')
{
alert('Textbox has a value');
}

//2nd way
if(txt_emp_salary.value.length>0)
{
alert('Textbox has a value');
}
}

What are the types used in JavaScript?

We have String,Number,Boolean,Function,Object,Null and Undefined as a type in Javascript.We can use above for checking types in Javascript.
What are the ways of commenting a code or a statement in JavaScript?

We can use double forward slash(//) for single statement or line comment

For Example:-
//var name = 'Vishal'; 


We also have multi-line comments as
/*

var name = 'Vishal';
var surname = 'Kumar'
var address = 'Pune';
*/

How do you convert numbers between different bases in JavaScript?

We use the parseInt() javascript function,that takes a string as the first parameter,and the base as a second parameter.

So to convert hexadecimal 5F to decimal,we can write as:-
parseInt("5F",16);

In a pop-up browser window,how do we refer to the main browser window that opened child window?

We use window.opener to refer to the main window from pop-ups,meaning that in the child window or pop-up window,we write window.opener to get main or base window' controls.
What is the data type of variables of in JavaScript?

All variables which are used in Javascript are treated as Object type.Means whatever variables we define inside javascript function using Var are said to be an Object type.
How to set the default focus in an element using Javascript?

<script type="text/javascript" language = "javascript"> function set_default_focus() 

{
document.forms[0].elements["txt_first_name"].focus();
}
</script>

What looping structures does JavaScript support?

Javascript support for-loop,while loop and do-while loops, but does not support foreach loop.

For Example:-

for(var index = 0;index<=5;index++)

{

}

How to close a pop-up window in Javascript?

We use self.close() or window.close() method to close any pop-up window:-

<a href='javascript:window.close()'>X</a>


//Or

<a href='javascript:self.close()'>X</a>

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories