Fix a javascript function

0

Greetings guys I have the following code

function botonver(a, b) {
imgnormal = "media/imagen/pdf.png";
tituloboton = "Ver PDF ";
botonpdf = "<img class = 'clase2' src = '" + imgnormal + "' width = '50%' />";
iniciotb = 4;

    return "<a title  = '" + tituloboton + b.substring(iniciotb) + "' target = '_blank' href = '" + a + "' >" + botonpdf + "</a>";
}

I would like to know how I can simplify it to use fixed variables file and title, the only thing that varies is the number and it will always be the same number that is when you want to show the button of file01 the title will be of the same number ie file01, title01

Just like this I must place the outputs as follows:

botonver(archivo07, titulo07) 

But I would like to know how I can implement to make an exit with a shorter code something like:

botonver(07)

An example of the output I have running is this:

function leccion02() {

    var archivo02 = ruta[02] + "Como hacer un Inicio.pdf";

    var titulo02 = "Hacer un Inicio Perfecto";

    contenidos = [
        {  a: "<h4 class = 'media-heading'>" + titulo02 + "</h4> texto.",
        b: botonver(archivo02, titulo02)
        },
    ];

    resultado = "";
    resultado += cabecera;

    for (key in contenidos) {

        resultado += "<div class = 'row well well-lg '> <div class = 'col-xs-12 col-sm-6 text-justify'>" + contenidos[key].a + "</div> <div class = 'col-xs-12 col-sm-6 text-center '>" + contenidos[key].b + "</div> </div>";

    };
    resultado += "</div> </div>";

    document.getElementById('contenido').innerHTML =
    resultado;

}

But I do not know how to make it work as I described it in my practice

    
asked by Jose M Herrera V 11.03.2018 в 03:08
source

1 answer

0

First, you will have to pass your value as a string to the function. Trying to pass it as a number will be much more problematic, since JS will remove the zero from the left.

Then, you use the number to concatenate it with the value of tituloboton and in the href you write archivo and concatenate with the num parameter dynamically.

Something like this:

botonver("07");

function botonver(num) {
  imgnormal = "media/imagen/pdf.png";
  tituloboton = "Ver PDF " + num;
  botonpdf = "<img class = 'clase2' src = '" + imgnormal + "' width = '50%' />";
  //iniciotb = 4;
  var elLink = "<a title  = '" + tituloboton + "' target = '_blank' href = 'archivo" + num + "' >" + botonpdf + "</a>";
  console.log(elLink);
  return elLink;
}

Another way to do it would be by using document.CreateElement . In that case, you would create a a element (link) and then assign the data to each attribute of the element.

The code is much clearer like this:

botonver("07");

function botonver(num) {
  imgBoton = "<img class = 'clase2' src = 'media/imagen/pdf.png' width = '50%' />";
  var elLink = document.createElement('a');
  elLink.textContent = imgBoton;
  elLink.href = 'archivo' + num;
  elLink.title = "Ver PDF " + num;
  elLink.target = "_blank";

  console.log(elLink);
  return elLink;
}
    
answered by 11.03.2018 / 03:32
source