Using buttons to swap image Javascript html - Stack Overflow

I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.

I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.

I have a very simple operation which allows for the click of a button to load a specific image in place of a default image. As oppose to having many functions which do the same thing, I'd rather have just one.

I imagine you can store the images in an array and select them by position?

Here is what I have so far.

function swap1() {
  document.getElementById("default").src="321.jpg";
}

function swap2() {
  document.getElementById("default").src="432.jpg";
}

function swap3() {
  document.getElementById("default").src="742.jpg";
}

 

<input type="button" onClick="swap1()">
<input type="button" onClick="swap2()">
<input type="button" onClick="swap3()">

I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.

I have a very simple operation which allows for the click of a button to load a specific image in place of a default image. As oppose to having many functions which do the same thing, I'd rather have just one.

I imagine you can store the images in an array and select them by position?

Here is what I have so far.

function swap1() {
  document.getElementById("default").src="321.jpg";
}

function swap2() {
  document.getElementById("default").src="432.jpg";
}

function swap3() {
  document.getElementById("default").src="742.jpg";
}

 

<input type="button" onClick="swap1()">
<input type="button" onClick="swap2()">
<input type="button" onClick="swap3()">
Share Improve this question edited Jul 27, 2013 at 22:59 Dennis 59.7k27 gold badges148 silver badges142 bronze badges asked Jul 27, 2013 at 22:42 dpalmdpalm 1431 gold badge3 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

use function parameters:

function swap(imgNumber)
{
document.getElementById("default").src=imgNumber+".jpg";
}

and later:

<input type="button" onclick="swap('321')">
<input type="button" onclick="swap('432')">
<input type="button" onclick="swap('742')">

To answer the ment (not very best js code but to show idea of variables):

var clip=false;
function setClip(val) {
   clip=val;
}
function swap(imgNumber)
{
   if(clip==true)
     document.getElementById("default").src=imgNumber+"clip.jpg";
  else
     document.getElementById("default").src=imgNumber+".jpg";
 }


<input type="button" onclick="swap('321')">
<input type="button" onclick="swap('432')">
<input type="button" onclick="swap('742')">
<input type="button" onclick="setClip(true)">

Simply you add variable which takes true/false - last button set clip variable to true - we changed swap function to check if clip is true - if yes - it loads different file

You could do this

JS

function swap(string img)
{
    document.getElementById("default").src=img;
}

HTML

<input type ="button" onClick="swap('321.jpg')">
<input type ="button" onClick="swap('432.jpg')">
<input type ="button" onClick="swap('742.jpg')">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967025a4603742.html

相关推荐

  • Using buttons to swap image Javascript html - Stack Overflow

    I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信