Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Monday, January 27, 2014

Switch condition with checkbox

<script>
function test() {
var cboxes = document.getElementsByName('option_check');
var len = cboxes.length;
for (var i=0; i<len; i++) {
if (cboxes[i].checked){var value=cboxes[i].value;}else{var value='Off';}
alert(value);
}
}
</script>
<input onClick="test()" type="checkbox" value="On" name="option_check">

to add and remove class of html tag element

<style type="text/css">
.btn_dwn_active{........}
</style>

<div id="tabs">
<div onClick=" $('#tabs div').removeClass ('btn_dwn_active'); $(this).addClass ('btn_dwn_active');">
</div></div>

to creat thumbnail news

<script type='text/javascript'>
var thumbnail_mode = "yes"; //yes -with thumbnail, no -no thumbnail
summary_noimg = 250; //summary length when no image
summary_img = 90; //summary length when with image
img_thumb_height = 130;
img_thumb_width = 70;
</script>
<script type='text/javascript'>
function removeHtmlTag(strx,chop){
if(strx.indexOf("<")!=-1)
{
var s = strx.split("<");
for(var i=0;i<s.length;i++){
if(s[i].indexOf(">")!=-1){
s[i] = s[i].substring(s[i].indexOf(">")+1,s[i].length);
}
}
strx = s.join("");
}
chop = (chop < strx.length-1) ? chop : strx.length-2;
while(strx.charAt(chop-1)!=' ' && strx.indexOf(' ',chop)!=-1) chop++;
strx = strx.substring(0,chop-1);
return strx+'...';
}

function createSummaryAndThumb(pID){
var div = document.getElementById(pID);
var imgtag = "";
var img = div.getElementsByTagName("img");
var summ = summary_noimg;
if(thumbnail_mode == "yes") {
if(img.length>=1) {
imgtag = '<span style="float:left; padding:0px 10px 5px 0px;"><img src="'+img[0].src+'" width="'+img_thumb_width+'px"/></span>';
summ = summary_img;
}
}
var summary = imgtag + '<div>' + removeHtmlTag(div.innerHTML,summ) + '</div>';
div.innerHTML = summary;
}
</script>
----------------------------

Saturday, December 21, 2013

to prevent submit form and show alert, before completing the form

<script>
String.prototype.toProperCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
function check_kosong(){
els=['input_id_1','input_id_2','input_id_3']
for (var i_tem = 0; i_tem < els.length; i_tem++){
var check=document.getElementById(els[i_tem]).value;
if (check == ''){
var a=els[i_tem];
//replace underscore with space
var b=a.replace(/_/g, ' ');
//show alert with convert text to Proper Case/ Title Case
alert (b.toProperCase() +' belum di isi');
return false;
i_tem=els.length;
}
}
}
</script>
<form onSubmit="return check_kosong();" action="../insert_your_file.php" method="post">
......
......
.......
</form>

Tuesday, December 3, 2013

key for get multiple Id of element

els=['nama_lengkap_alert','nama_panggilan_alert']
for (var i_tem = 0; i_tem < els.length; i_tem++)
document.getElementById(els[i_tem]).style.visibility='visible';

real sintax to call PHP function from Javascript

<?php 
function writeName()
{
echo "One Solution";
}
?>
onClick="javascript:document.write('<?php writeName();?>');"

to print iframe content in HTML

<iframe class="showform" id="showform" name="showform" src="desktop.php"></iframe>
<script>
function printiframe() {
window.frames["showform"].focus();
window.frames["showform"].print();
}
</script>