Using If Else condition
<script>
function change(t){
if(t.src=="http://www.w3schools.com/js/pic_bulboff.gif")
t.src="http://www.w3schools.com/js/pic_bulbon.gif"
else
t.src="http://www.w3schools.com/js/pic_bulboff.gif"
}
</script>
<body>
<img src="http://www.w3schools.com/js/pic_bulboff.gif" onclick="change(this)" name="mani"/>
</body>
<script>
function change(t)
{
t.src = t.src=="http://www.w3schools.com/js/pic_bulboff.gif" ? "http://www.w3schools.com/js/pic_bulbon.gif" : "http://www.w3schools.com/js/pic_bulboff.gif"
}
</script>
<body>
<img src="http://www.w3schools.com/js/pic_bulboff.gif" onclick="change(this)" name="mani"/>
</body>
<script>
function change(t){
if(t.src=="http://www.w3schools.com/js/pic_bulboff.gif")
t.src="http://www.w3schools.com/js/pic_bulbon.gif"
else
t.src="http://www.w3schools.com/js/pic_bulboff.gif"
}
</script>
<body>
<img src="http://www.w3schools.com/js/pic_bulboff.gif" onclick="change(this)" name="mani"/>
</body>
Same example using ternary operators
function change(t)
{
t.src = t.src=="http://www.w3schools.com/js/pic_bulboff.gif" ? "http://www.w3schools.com/js/pic_bulbon.gif" : "http://www.w3schools.com/js/pic_bulboff.gif"
}
</script>
<body>
<img src="http://www.w3schools.com/js/pic_bulboff.gif" onclick="change(this)" name="mani"/>
</body>
Demo
0 comments:
Post a Comment