Monday, 6 February 2017

jquery to show and hide div content - easy method

Here is a simple script using jQuery library to hide and show the div content. We are using built in function toggle() and if condition to show and hide the content respectively.

<!DOCTYPE html>
<html><head></head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.Add').click(function(){
    $('.hidden', this).toggle();
    if($('.hidden').is(":visible")){
        $('.Add p').text("- close content");
    }
     else 
    {
        $('.Add p').text("+ Open content"); 
    } 
  });
});
</script>
<div class="Add">
    <p>
    + Open content
    </p>
    <div class='hidden' style="display:none;">Here goes your hidden content</div>
</div>
</body>
</html>

Try Output Here :

+ Open content

Read More

JavaScript function to find a character and print the position of it

Here is a script which prints the position of the character in a given string. Here using charAt() built in function to traverse the characters.

<!DOCTYPE html>
<html>
<body>
<lable>Enter the string here : </lable>
 <input type="text" name="enter" id="text" value=""/>
<br/>
<lable>Enter the character to find : </lable>
 <input type="text" name="enter" id="char1" value=""/>
<br/>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() 
{
var str =  document.getElementById('text').value;
var len = str.length;
var mychar = document.getElementById('char1').value;
var k=0;
for(var i=0; i<len;i++)
 {
 search = str.charAt(i);
    if (search == mychar)
    {
     alert("Found at position = " + i);
    }
 }
}
</script>
</body>
</html>

Try Output Here :
Enter the string here :


Enter the character to find :



Read More

Javascript function to print the Unicode of the character

The below script displays the Unicode of the given string using JavaScript. Here using charCodeAt() built in function to process each letter of text.

<!DOCTYPE html>
<html>
<head>
<script>
function myFun(){
var str = "abcd";
var len = str.length;
for(i=0;i<len;i++)
{
var str2 = str.charCodeAt(i);
alert(str2);
}
}
</script>
</head>
<body>
<button onclick="myFun()">Click Here</button>
</body>
</html>

Try Output Here :
Read More

Javascript function to read the each characters in a string

This is simple method to traverse a string char by char using JavaScript. Here using charAt() built in function to process each letter of text.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(){
var str = "abcd";
var len = str.length;
for(i=0;i<len;i++)
{
var str2 = str.charAt(i);
alert(str2);
}
}
</script>
</head>
<body>
<button onclick="myFunction()">Click Here</button>
</body>
</html>

Try Output Here :
Read More

Javascript function to reverse a string - Easy Method


<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(){
var str = "abcd";
var len = str.length;
for(i=len-1;i>=0;i--)
{
var str2 = str.charAt(i);
alert(str2);
}
}
</script>
</head>
<body>
<button onclick="myFunction()">Click Here</button>
</body>
</html>
Try Output Here :

Read More

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016