Javascript date and time, Indian date format, todays date
date format d-m-Y H:i:s
var date = new Date();
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
var H= date.getHours();
var i= date.getMinutes();
var s= date.getSeconds();
var mydate=d+'-'+m+'-'+y+' '+H+':'+i+':'+s;
add background image with javascript
<div id="tab1" style="position:relative; background-image:url(buttons/off.png);
<a href="javascript:ChangeBackgroundImageOfTab('tab1', 'on');">
<img id="DivBtn1" name="DivBtn1" src="buttons/text.png" >
</a>
</div>
<script type="text/javascript">
function ChangeBackgroungImageOfTab(tabName, imagePrefix)
{
var urlString = 'url(buttons/' + imagePrefix + '.png)';
document.getElementById(tabName).style.backgroundImage = urlString;
}
</script>
How to set a buttons innerHTML as an image?
<button id="button">Notepad</button>
<script type="text/javascript">
$(document).ready(function() {
var btn = document.getElementById("button");
btn.style.backgroundImage = "url('/media/notepad.jpg')";
});
</script>
How to validate an email address in JavaScript
<form>
<p>Enter an email address:</p>
<input id='email'>
<button type='submit' id='validate'>Validate!</button>
</form>
<h2 id='result'></h2>
<script type="text/javascript">
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function validate() {
const $result = $("#result");
const email = $("#email").val();
$result.text("");
if (validateEmail(email)) {
$result.text(email + " is valid :)");
$result.css("color", "green");
} else {
$result.text(email + " is not valid :(");
$result.css("color", "red");
}
return false;
}
$("#validate").on("click", validate);
</script>
How to generate a simple popup contact us using jQuery
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="messagepop pop">
<form method="post" id="new_message" action="/messages">
<p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
<p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
<p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
</form>
</div>
<a href="/contact" id="contact">Contact Us</a>
<script type="text/javascript">
$(document).ready(function() {
$("#contact").dialog();
});
</script>
include http://docs.jquery.com/UI/Dialog
Note:dont need any css if using jquery dialog
<style type="text/css">
a.selected {
background-color:#1F75CC; color:white; z-index:100;
}
.messagepop {
background-color:#FFFFFF;
border:1px solid #999999;
cursor:default;
display:none;
margin-top: 15px;
position:absolute;
text-align:left;
width:394px;
z-index:50;
padding: 25px 25px 20px;
}
label {
display: block;
margin-bottom: 3px;
padding-left: 15px;
text-indent: -15px;
}
.messagepop p, .messagepop.div {
border-bottom: 1px solid #EFEFEF;
margin: 8px 0;
padding-bottom: 8px;
}
</style>
How to use check all checkboxes using jQuery?
<script type="text/javascript">
$(document).ready(function() {
$('#checkedAll').click(function () {
$('input:checkbox').prop('checked', this.checked);
});
});
</script>
<div class="checkall">
<table><tr><td>
<input type="checkbox" name="checkedAll" id="checkedAll" /> Check All</td></tr>
<tr><td><input type="checkbox" name="checkAll" class="checkSingle" /> Cat</td></tr>
<tr><td><input type="checkbox" name="checkAll" class="checkSingle" /> Dog/td></tr>
<tr><td><input type="checkbox" name="checkAll" class="checkSingle" /> Cow</td></tr>
</table></div>
How to select all checkboxes using jQuery?
<form>
<table>
<tr>
<td><input type="checkbox" id="select_all" /></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]" /></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]" /></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#select_all').change(function() {
var checkboxes = $(this).closest('form').find(':checkbox');
checkboxes.prop('checked', $(this).is(':checked'));
});
});
</script>