This can be achieved by using .length
if($("#id_name").length == 0) {
//the element with id_name does not exists
}
This can be achieved by using .length
if($("#id_name").length == 0) {
//the element with id_name does not exists
}
If users is the id of the select Chosen dropdown box, then use JQuery to append new options dynamically as in the example below
$("#users").append('<option value="1">User</option>').trigger("chosen:updated");
.trigger(“chosen:updated”); refreshes the Chosen dropdown box with the new option.
This can be done using the jQuery length property
You will use the following command to determine if there are any options within the select box.
var length = $('#selectBoxId > option').length;
console.log(length);
#selectBoxId should be replaced with the ID of your select box.
I recently had a problem trying to load a PHP page into a DIV tag using Ajax. Everything worked fine in Firefox, Opera, Safari and Google Chrome, but there was no way for it to work in Internet Explorer 7. Personally I don’t like Internet Explorer due to amount of problems I get while developing. I tried to load the page into a DIV using JQuery. JQuery failed even thougt it’s supposed...
Javascript gives you the ability to open new windows in you’re browser. You’ll also have the ability to run any code in these windows, you could even have actions in the one window that will affect another window. The following statement is used to open up a basic popup window window.open(“popup.html”,”PopUp...
window.location.href = 'redirect.html';
By using the isNaN() JavaScript function. isNaN = Is Not A Number
Examples:
document.write(isNaN(“Hello World!”)); //Will print true since Hello World! is not a number
document.write(isNaN(123456)); //Will print false, since 123456 is a number.
var phoneNumber = 0724956785234;
if(isNaN(phoneNumber) == true){
alert(“Please enter a valid phone number”);
return false;
}
There are two JavaScript function that you can use to accomplish this
encodeURIComponent()
Encodes a URI component
Encodes special characters
The following characters: , / ? : @ & = + $ # are encoded
var url=""+encodeURIComponent(url);
encodeURI()
Encode a URI
Encodes special characters but not (except) , / ? : @ & = + $ #
var url=""+encodeURI(url);
var iframe = document.getElementById('iframe_id');
iframe.src = iframe.src;
iframe_id is the ID of the IFrame
document.getElementById('iframe_id').src = 'newpage.html
iframe_id = The ID for the IFrame
newpage.html = The page that you want to load into the IFrame
If we want to call a function every 10 seconds using JavaScript we can accomplish it by implementing the following piece of code window.setInterval('yourfunction()', 1000); function yourfunction(){ alert('test'); } With the code above every 10 seconds a JavaScript function with the name yourfunction will be called that will prompt an alert. window.setInterval = Evaluates an expression each...
To enable a form field use the below JavaScript
document.getElementById("ELEMENT_ID").disabled = false;
And to disable the form field use the below JavaScript
document.getElementById("ELEMENT_ID").disabled = true;
NOTE: Replace ELEMENT_ID with the form fields ID
var filename = 'myFile.jpg';
var ext = filename.split('.').pop(); //We split the filename on every instance where the . is found and then getting the last element in the array using the pop() function
alert(ext);
Add the following JavaScript function to your HTML page function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } The above function checks the input of the user and then returns false if no number has been entered and true if a number has been...
Use the string.toLowerCase() function as the example below
<script type="text/javascript">
var str="I NEED THIS STRING TO BE CONVERTED TO LOWER CASE";
document.write(str.toLowerCase());
</script>
Use the history.go(-1) function in conjunction with the anchor html element
Example
<a href="javascript: history.go(-1);">Go back to previous page</a>
If you have a text input box with ID age and you want to obtain the value for the text box via JavaScript use the JavaScript function getElementById() to get the value
Html input text box
<input type="text" name="age" id="age" value="12" />
JavaScript function
<script type="text/javascript">
var age = document.getElementById('age').value;
</script>
If you have a JSON encoded string that you might have received from a ajax scriptand you need to decode it using JavaScript you can use the eval() function to decode the string. The eval() function evaluates or executes an given argument.
Example:
var string = '{"id" : 32}';
var d = eval('(' + string + ')');
alert(d.id); //will alert 32
This can be obtained by using the Pre-defined object (Date) in JavaScript
Example:
document.write("The current date is:"+Date())