Adrihin
Adrihin

Reputation: 23

Copy href from <a> inside a DIV with unique id and paste somewhere else in that DIV

I am trying to find a way to copy the href from the data-name="entity_field_post_title" div, and use it as the href of data-type="image" div

The goal behind this is to open the corresponding link of the page instead of opening it's photos. These are miniatures of posts, and I would like to open the post themselves, not the photos.

Important to point out that the top div's ID number changes each time unpredictably.

<div id="drts-content-post-1163">
    <div class="drts-display-element-columns-3">
        <div class="drts-row drts-gutter-none">
            <div class="slick-list draggable">
                <div class="slick-track">
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/horse.jpeg" ><figure><img src="http://localhost/wp/wp-content/uploads/horse.jpeg"></figure></a>
                    </div>
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/panda.png" ><figure><img src="http://localhost/wp/wp-content/uploads/panda.png"></figure></a>
                    </div>
                </div>          
            </div>
        </div>
        <div data-name="column" class="drts-display-element-column-6">
            <div class="drts-row drts-gutter-none">
                <div data-name="entity_field_post_title" class="directory-listing-title ">
                    <a href="http://localhost/wp/anuncios/prueba-desde-backend-1/" target="_self" class="drts-entity-1163">TEXT</a>
                </div>
            </div>
        </div>
    </div>
</div>

Upvotes: 1

Views: 146

Answers (3)

Adrihin
Adrihin

Reputation: 23

Thanks to Light the solution was found. First part is to detect whether JS and jQuery was working in Wordpress.

// Select all the top level divs, without using ID

alert("JS works");
if (jQuery) {  
  alert('Jquery Loaded');
} else {
  alert('Jquery Not Loaded');
}

This is how jQuery is set in Wordpress because the $ is set as a function otherwise. I created "linko" to understand what was happening inside the .find()

// jQuery code:
jQuery( function( $ ){

  // Safely use the $ alias here
  // Code here will be executed on the document.ready event
  let containerDiv = 'div[id*="drts-content-post-"]';
  let linkDiv = 'div[data-name="entity_field_post_title"]';
  let imageDiv = 'div[data-type="image"]';

  let $container = $(containerDiv).has(linkDiv);

  $container.each((index, element) => {
    let link = $(element).find(`div[data-name="entity_field_post_title"] > a`).attr('href');
    console.log(link);
    let linko = $(element).find(`div[data-type="image"] > a`).attr('href');
    console.log(linko);

    $(element).find(`div[data-type="image"] > a`).attr('href', link);
    console.log($(element).find(`div[data-type="image"] > a`).attr('href'));
  })
});

Upvotes: 0

Shiny
Shiny

Reputation: 5055

I've left the selectors as variables just in case you need to make minor changes to those, but the base works - Looping through every containerDiv, getting the href from linkDiv > <a>, and applying that link to every imageDiv > <a>

let containerDiv = 'div[id*="drts-content-post-"]';
let linkDiv = 'div[data-name="entity_field_post_title"]';
let imageDiv = 'div[data-type="image"]';

let $container = $(containerDiv).has(linkDiv);

$container.each((index, element) => {
  let link = $(element).find(`${linkDiv} > a`).attr('href');
  $(element).find(`${imageDiv} > a`).attr('href', link);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="drts-content-post-1163">
    <div class="drts-display-element-columns-3">
        <div class="drts-row drts-gutter-none">
            <div class="slick-list draggable">
                <div class="slick-track">
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/horse.jpeg" ><figure><img src="http://localhost/wp/wp-content/uploads/horse.jpeg"></figure></a>
                    </div>
                    <div data-type="image" class="slick-slide">
                        <a href="http://localhost/wp/wp-content/uploads/panda.png" ><figure><img src="http://localhost/wp/wp-content/uploads/panda.png"></figure></a>
                    </div>
                </div>          
            </div>
        </div>
        <div data-name="column" class="drts-display-element-column-6">
            <div class="drts-row drts-gutter-none">
                <div data-name="entity_field_post_title" class="directory-listing-title ">
                    <a href="http://localhost/wp/anuncios/prueba-desde-backend-1/" target="_self" class="drts-entity-1163">TEXT</a>
                </div>
            </div>
        </div>
    </div>
</div>

Upvotes: 2

Gandalfion
Gandalfion

Reputation: 17

I am not sure I understand but if you just want to make the picture into a link you can use an anchor-tag with a background picture like this:

<a class="photo" href="website.net/link" title="photo" id="photo">photo</a>

Upvotes: 0

Related Questions