Lists in HTML5 and JS [closed]

0

I want to make a list that when selecting one of its elements, an image related to that element appears on the same page, when selecting another element, the image of the second element appears and with the other elements, so that the images appear at the end of the selected elements either 1 or as many times as I have selected it, it came out with onclick, but I have to do it with a function in JS.

This is my code:

var forma = document.getElementById("forma"),
Ferrari = document.getElementById("Accion1"),
BMW = document.getElementById("Accion2"),
AUDI = document.getElementById("Accion3"),
Lamborghini = document.getElementById("Accion4"),
AlfaRomeo = document.getElementById("Accion5"),
Maserati = document.getElementById("Accion6"),
Tesla = document.getElementById("Accion7"),
forma.addEventListener("click", carros, false);

carros();

function carros() {

}
<header>
 <h1>Examen 2</h1>
</header>

<form id="forma">

  <fieldset>
    <legend>Selecciona un carro.</legend>

    <ul>
        <li id="Accion1"><a href="#" >Ferrari</a></li>    
        <li id="Accion2"><a href="#" >BMW</a></li>    
        <li id="Accion3"><a href="#" >AUDI</a></li>    
        <li id="Accion4"><a href="#" >Lamborghini</a></li>    
        <li id="Accion5"><a href="#" >Alfa Romeo</a></li>    
        <li id="Accion6"><a href="#" >Maserati</a></li>   
        <li id="Accion7"><a href="#" >Tesla</a></li>   
    </ul>
    <hr />

  </fieldset>

</form>
<script  src="js/ctrlAutorizacion.js"></script>
    
asked by Joseph Tapia Garza 26.04.2017 в 02:26
source

3 answers

1

I would like to help you, first you should guide me in your doubt.

  • First: I assume that the images are in a folder located in the same directory as the html document.
  • Second: somehow you must relate the option to each image, either by the text of the li (the image corresponding to li with id= action1 should be called ferrari.jpg ), or with some attribute of the element li (Example id='action' data='Ferrari' )

  • Third: create a div to add the corresponding images and assign a id

  • You must associate an event handler with the li elements of your document using addeventlistener , for example:

    document.getElementsByTagName("LI").addEventListener("click", function()
    {
         //aquí, en el cuerpo de la función agregaríamos las imágenes al div 
         // mencionado en el tercer item
    });
    
  • Fourth: you create an element img and assign the property src with the related image (here it depends on how you get the name of the image, what attribute of li you get, the attribute data or the text of li ) (this is where I have a doubt, since I use jquery and I am not sure that the syntax is correct, in the body of the function of the previous point you must obtain the element where the property was clicked what interests you, example:

    var nombreImagen=this.getAttribute("data")+".jpg";
    
    var elem = document.createElement("img");
    
    elem.setAttribute("src", nombreImagen);
    
  • fifth: you get the div element by its id and you add a child node created before document.getElementById("idDivPunto3").appendChild(elem);

    I hope it serves to guide you, all the code I did in the air. I think it is a way to reach your goal, surely they will make corrections that will serve you.

answered by 26.04.2017 в 06:39
0

I finished it. I leave the code in case it serves someone:

function imagen(valor){
    if(valor === 'ferrari'){
        document.getElementById('carro').innerHTML = '<img src="img/ferrari.jpg" alt="Ferrari"/>';
    }
    else if(valor === 'mercedes'){
        document.getElementById('carro').innerHTML = '<img src="img/mercedes.jpg" alt="Mercedes"/>';
    }
    else if(valor === 'lamborghini'){
        document.getElementById('carro').innerHTML = '<img src="img/lambo.jpg" alt="Lamborghini"/>';
    }
    else if(valor === 'mclaren'){
        document.getElementById('carro').innerHTML = '<img src="img/mclaren.jpg" alt="McLaren"/>';
    }
    else if(valor === 'tesla'){
        document.getElementById('carro').innerHTML = '<img src="img/tesla.jpg.jpg" alt="Tesla"/>';
    }
    else{
        document.getElementById('carro').innerHTML = '<img src="" alt=""/>';
    }
}
<header>
  <h1>Carros</h1>
</header>
<form>
    <fieldset style="text-align: center;">
    <legend>Selecciona un carro para verlo.</legend>
    <p>
        <label accesskey="C">
            Carros:<br>
            <select name="carros" required size="6" onchange="imagen(this.value)">
                <option value="0" selected="selected">Seleccione</option>
                <option value="ferrari">Ferrari F40</option>
                <option value="mercedes">Mercedes SLR</option>
                <option value="lamborghini">Lamborghini Veneno</option>
                <option value="mclaren">McLaren P1</option>
                <option value="tesla">Tesla Hypercar</option>
            </select>
        </label>
    </p>
    <br>
    <div id="carro">
  </div>
  </fieldset>
</form>
    
answered by 26.04.2017 в 06:03
0

According to your answer so that there are not so many nested if you might want to use a switch

function imagen(valor){
   var val = valor;
   switch(val){
      case 'tesla':
      document.getElementById('carro').innerHTML = '<img src="img/mercedes.jpg" alt="Mercedes"/>';
      break;
      case 'mclaren':
      document.getElementById('carro').innerHTML = '<img src="img/mclaren.jpg" alt="McLaren"/>';
      break;
      default:
      document.getElementById('carro').innerHTML = '<img src="" alt=""/>';
      break;
   }
}
    
answered by 26.04.2017 в 14:30