Reputation: 5
This is my first question I'm asking. Is it possible to change the onclick="change()"
to another function like onclick="change1()"
My code is like this:
<a href="#" onclick="changeSrc()" id="a1"><img src="images/transparent-arrow-hi.png" style="position:absolute; top:525px; left:1000px; height:50px; width:50px;" ></a>
<img src="images/keyboard.png" id="pic1">
<img src="images/mouse1.png" id="pic2">
<img src="images/PCstand.png" id="pic3">
<img src="images/monitor.png" id="pic4">
My Javascript is as follows:
<script>
function changeSrc() {
document.getElementById('pic1').src="images/robber1.jpg";
document.getElementById('pic2').src="images/guarding entrance 1.jpg";
document.getElementById('pic3').src="images/Lock.ico";
document.getElementById('pic4').src="images/Number pads.jpg";
}
</script>
This works so far in changing the img src , But I want to click the same button many times to change the img src to others.
So its like this button that when onclick , can call the first function to change the img src and at the same time prepare the onclick to call the second funtion.
Is that even possible?
I can add more code if needed.
Upvotes: 0
Views: 206
Reputation: 1816
you can have counter and every time call your function increase number of counter and select what actions you need.or you can use switch in changeSrc function
<script>
var counter = 0;
function changeSrc() {
if(counter==0){
document.getElementById('pic1').src="images/robber1.jpg";
document.getElementById('pic2').src="images/guarding entrance 1.jpg";
document.getElementById('pic3').src="images/Lock.ico";
document.getElementById('pic4').src="images/Number pads.jpg";
}
if(counter==1){
document.getElementById('pic1').src="images/robber2.jpg";
document.getElementById('pic2').src="images/guarding entrance 2.jpg";
document.getElementById('pic3').src="images/Lock2.ico";
document.getElementById('pic4').src="images/Number pads2.jpg";
}
counter++;
}
</script>
if your images have pattern like numbers you can do this
<script>
var counter = 0;
function changeSrc() {
document.getElementById('pic1').src="images/robber"+counter+".jpg";
document.getElementById('pic2').src="images/guarding entrance "+counter+".jpg";;
document.getElementById('pic3').src="images/Lock "+counter+".ico";;
document.getElementById('pic4').src="images/Number pads"+counter+".jpg";;
counter++;
}
</script>
Upvotes: 2
Reputation: 30739
You can use this code snippet to dynamically associate and change the onclick
function of the element.
function changeSrc(_this){
//associate function change1 inside changeSrc
$(_this).attr('onclick','change1(this)');
}
function change1(_this){
//associate function change2 inside change1
$(_this).attr('onclick','change2(this)');
console.log('Iniside Change1()')
}
function change2(_this){
//associate function change3 inside change2
$(_this).attr('onclick','change3(this)');
console.log('Iniside Change2()')
}
function change3(_this){
console.log('Iniside Change3()')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="changeSrc(this)" id="a1"><img src="images/transparent-arrow-hi.png" style="position:absolute; top:525px; left:1000px; height:50px; width:50px;" >click</a>
Upvotes: 0