JS - Get and Insert URL

72 views Asked by At
var divs = document.getElementsByClassName('clsItemBlock');

for (var i = 0; i < divs.length; i++) {

    var iDiv = document.createElement('div');

    iDiv.id = 'detail_button';

    document.getElementsByClassName('clsItemPublished')[i].appendChild(iDiv);

    iDiv.innerHTML = '<a href="###">View Details</a>';
}

The code above works and adds a 'View Detail' button to every product on a category page. What I'm need to now do with this is get the URL from another element and swap out what is currently ### in the example above.

It is essentially the first href inside of the class "clsItemMoreDetails". I've found chunks of code to do some pieces, but can't seem to piece it all together.

EDIT

Here is the HTML:

<div class="clsItemMoreDetails">
    <a title="Strapless Ruffle Dress" class="onlineUser" href="http://www.runwaycrush.com/women/dresses/strapless-ruffle-dress.html">
        <p class="clsItemHead">Strapless Ruffle Dress</p>
    </a>
    <div class="clsCategorydateBlock clsOverflow">
        <p class="clsItemCategory">
            <a href="http://www.runwaycrush.com/shop/ocean-avenue.html" title="Check out OceanAvenue's store">
                <span>OceanAvenue</span>
            </a>
        </p>
        <p class="clsItemPublished">$68.24
            <span>USD</span>
        </p>
    </div>
</div>
1

There are 1 answers

3
zmii On BEST ANSWER

Theoretically you should do something like this:

var url = document.getElementById("#id_of_some_element").href;
iDiv.innerHTML = '<a href="' + url + '">View Details</a>';

Presuming that other element has href property.