Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
Randomize Picture with relative text javascript

I would like to display a random picture with a relative text everytime I load the page. So in order to have Image1 with text1, Image2 with text2 ....

Here is my code :

html :

   <link href="app.css" rel="stylesheet" />
    <script src="random-picture.js"></script>
    
    <div class="
              content-with-two-section-2 content-1
              padding-top-8 grey-background
              
            ">
          <div class="hero-content-with-image">
            <img id="randomImages" alt="hero-image">
          </div>
    
          <div class="">
            <div id="" class="section-text-1">
                <h1>RANDOM 1
                  <p>random1</p>
            </div>
           </div>
    <div class="">
            <div id="" class="section-text-2">
                <h1>RANDOM 2
                  <p>random2</p>
            </div>
           </div>
    <div class="">
            <div id="" class="section-text-3">
                <h1>RANDOM 3
                  <p>random3</p>
            </div>
           </div>
    </div>

I put all classes as display:none; on my css file :

.section-text-1, .section-text-2, .section-text-3 {
  display: none; 
}

And this is my js file :

var image = new Array();
image[0] = "image1.png";
image[1] = "image2.png";
image[2] = "image3.png";
var size = image.length
var x = Math.floor(size * Math.random())

jQuery('#randomImages').attr('src', image[x]);

Thanks a lot for your help !!!!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do as follows:

Enumerating class names is not necessary (you can use '.section-text' alone, because after querying you will get an array with indexes corresponding to the elements order in DOM).

const sources = [
  'image1.png',
  'image2.png',
  'image3.png'
];

const img = document.getElementById('randomImages');
const sections = document.getElementsByClassName('section-text');

const index = Math.floor(sources.length * Math.random());

img.alt = sources[index];
img.src = sources[index];

sections[index].style.display = 'block';
.section-text {
  display: none; 
}
<div class="content-with-two-section-2 content-1 padding-top-8 grey-background">
  <div class="hero-content-with-image">
    <img id="randomImages">
  </div>
    
  <div>
    <div class="section-text">
      <h1>RANDOM 1</h1>
      <p>random1</p>
    </div>
  </div>

  <div>
    <div class="section-text">
      <h1>RANDOM 2</h1>
      <p>random2</p>
    </div>
  </div>

  <div>
    <div class="section-text">
      <h1>RANDOM 3</h1>
      <p>random3</p>
    </div>
  </div>
</div>

As an alternative (suggested in comments):

const sources = [
  {
    src: 'image1.png',
    title: 'Image 1',
    text: 'This is text 1'
  },
  {
    src: 'image2.png',
    title: 'Image 2',
    text: 'This is text 2'
  },
  {
    src: 'image3.png',
    title: 'Image 3',
    text: 'This is text 3'
  }
];

const image = document.getElementById('image');
const title = document.getElementById('title');
const text = document.getElementById('text');

const index = Math.floor(sources.length * Math.random());

const source = sources[index];

image.alt = source.src;
image.src = source.src;

title.innerText = source.title;

text.innerText = source.text;
<div class="content-with-two-section-2 content-1 padding-top-8 grey-background">
  <div class="hero-content-with-image">
    <img id="image">
  </div>
    
  <div>
    <div class="section-text">
      <h1 id="title"></h1>
      <p id="text"></p>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use some sort of shuffling algorithm to output random pictures.

function randomPic(arr){
    const arrLength = arr.length;
    for (let i = 0; i < 100; i++ ) {
      const indexOne = Math.floor(Math.random() * arrLength);
      const indexTwo = Math.floor(Math.random() * arrLength);

      
      const tempValue = arr[indexOne];
      arr[indexOne] = arr[indexTwo];
      arr[indexTwo] = tempValue
    }
    
    return arr
  }
  
  
  const sources = [
    {
      src: 'https://image1.png',
      title: 'Title 1',
      text: 'This is text 1'
    },
    {
      src: 'https://image2.png',
      title: 'Title 2',
      text: 'This is text 2'
    },
    {
      src: 'https://image3.png',
      title: 'Title 3',
      text: 'This is text 3'
    }
  ];
  
  console.log(randomPic(sources))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda