N
N
Natalia2018-02-01 16:24:23
JavaScript
Natalia, 2018-02-01 16:24:23

How to link to a div in jquery?

Good afternoon!

There is a js array
var myArray = [ "link":"url_1", "link":"url_2", "link":"url_3" ];

there is html:

<div> блок 1 </div>
<div> блок 2 </div>
<div> блок 3 </div>


you need to use jquery / or js to hang each block with its own link: block 1 - url_1, block 2 - url_2, block 3 - url_3.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2018-02-01
@webinar

I don’t understand where to shove the div url, but let’s say:

$('div').each(function(i,el){
       $(el).text(myArray[i].link);
});

api.jquery.com/jquery.each

D
Dima Polos, 2018-02-01
@dimovich85

I'm writing from a phone, so it's sketchy: div -> onclick window.location.href = "www.sample".

A
Artemy, 2018-02-01
Kaydash @kayart

var myArray = [ "url_1", "url_2", "url_3" ];

$('div').each(function(i) {
  $(this).wrap('<a href="' + myArray[i] + '"></a>');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question