Javascript: displaying images with checkboxes

I am currently working with radio buttons and checkboxes to display images using javascript. To be specific: I ran into a problem while working with checkboxes and displaying images. Using the radio buttons for this category, only one image will be displayed. But with checkboxes you need to display more than one image. For example, a user may check the jacket and glove box and both images will be displayed.

How can I format a function to display multiple images when I click the buttons? EXAMPLE

Js

<script language="JavaScript" type="text/javascript">

function check_value(val, id, type) {     
    var el = document.getElementById("imgBox" + id);
    if (val>0 && val<4) { //will trigger when [1,2,3]
       el.src = "images/"+ type + val + ".jpg";
       el.style.display = "";
  }    
  }      

</script>

HTML

<h2>Choose a bike</h2>
<form name="builder">
    <input type="radio" name="field" value="1" onclick='check_value(1, 1, "bike")'/> KAWASAKI KX 450F<br />
    <input type="radio" name="field" value="2" onclick='check_value(2, 1, "bike")'/> 2010 Yamaha Road Star S<br />
    <input type="radio" name="field" value="3" onclick='check_value(3, 1, "bike")'/> Aprilia RSV4<br />
</form>

<img id="imgBox1" src="#" style="display:none"> 

<h2>Choose a tire</h2>  
<form name="tire">
    <input type="radio" name="field" value="1" onclick='check_value(1, 2, "tire")'/> Michelin Pilot Road 3 Tires<br />
    <input type="radio" name="field" value="2" onclick='check_value(2, 2, "tire")'/> Dunlop Roadsmart Sport-Touring Tires<br />
    <input type="radio" name="field" value="3" onclick='check_value(3, 2, "tire")'/> Pirelli Scorpion Trail Tires<br />
</form>

<img id="imgBox2" src="#" style="display:none">

<h2>Choose Accesories</h2>
<form name="tire">
    <input type="checkbox" name="field" value="1" onclick='check_value(1, 3, "accesories")'/> Chrome Front Plate<br />
    <input type="checkbox" name="field" value="2" onclick='check_value(2, 3, "accesories")'/> Jacket<br />
    <input type="checkbox" name="field" value="3" onclick='check_value(3, 3, "accesories")'/> Gloves            
</form>

<img id="imgBox3" src="#" style="display:none">
+5
source share
4 answers

Add two more image windows:

<img id="imgBox3a" src="#" style="display:none">
<img id="imgBox3b" src="#" style="display:none">
<img id="imgBox3c" src="#" style="display:none">

Then call onclick:

check_value(1, "3a", "accessories");
check_value(2, "3b", "accessories");
check_value(3, "3c", "accessories");
+1

, ,

DIV,

      <div id="image">
     <img id="imgBox3" src="#" style="display:none"/>
       </div>

Script:

DIV

     var imag=document.createElement("img");
     imag.src="image";
     document.getElementById("image").appendChild(imag);
0
<form name="tire">
    <input type="checkbox" name="field" value="1" onclick='check_value(1, 3, "accesories")'/> Chrome Front Plate<br />
    <input type="checkbox" name="field" value="2" onclick='check_value(2, 3, "accesories")'/> Jacket<br />
    <input type="checkbox" name="field" value="3" onclick='check_value(3, 3, "accesories")'/> Gloves            
</form>
<div id='access'></div>    

function check_value(val, id, type) {     
        var img = document.createElement("img");
    img.src = "http://webprolearner2346.zxq.net/bike_calculator/images/"+type+val+".jpg";
    alert(img.src)
    var src = document.getElementById("access");
    src.appendChild(img);
      }   

fiddle

0

, , ... ..., onchange:

<h2>Choose Accesories</h2>
<form name="tire">
    <input type="checkbox" name="field" value="1" onchange='check_value(1, 3, "accesories",this)'/> Chrome Front Plate<br />
    <input type="checkbox" name="field" value="2" onchange='check_value(2, 3, "accesories",this)'/> Jacket<br />
    <input type="checkbox" name="field" value="3" onchange='check_value(3, 3, "accesories",this)'/> Gloves            
</form>

, : "this" . :

function check_value(val, id, type,mycheckbox) {
    if (mycheckbox.checked){
        //do something when it checked
    }else{
        //do something when it not checked.
    }
}

, ?

, @Sibu ( , ).

Another is to create pre-created image elements using "display: none" (with the src attribute already set), so the only thing you need to do is play with the display attribute.

Personally, I would say option2 is better, because css changes faster than manipulating dom.

0
source

All Articles