0

have a static links which is feeded with data from js function.

<div class="link">
     <a id="pdfdocument" class="button" target="_blank" href="#">Download starten</a>
     <a id="xlsdocument" class="button" target="_blank" href="#">Download starten</a>
</div>

JS

if (contentElement.name.indexOf(".xls") != -1) {
    "<a id='" + $("#xlsdocument").attr("href", "/dcontent?element=" + contentElement.id + "&handle=" + openBi.handle) + "</a>";
    $('#pubExcel').html(contentElement.filename);
} else {
    $("#pdfdocument").attr("href", "/dcontent?element=" + contentElement.id + "&handle=" + openBi.handle);
    $('#pubPdf').html(contentElement.filename);
}

my question is, how to create generic link if i have loaded more than one document. for example 4 documents = 4 links for pdf and 4 excel in html, 40 documents = 40 links for pdf and 40 excel in html.. struggeling on such a easy step

2
  • What does that 'contentElement' object contain? Your question is difficult to understand. Commented Apr 17, 2014 at 15:11
  • I'm already not following your second line. "Take a string (<a id='), add the result of changing the href of #xlsdocument, add another string, and then throw it away." Commented Apr 17, 2014 at 15:12

2 Answers 2

1

If you have an array of all files

var documents = ['file_one', 'file_two', 'file_three'];

Maybe this would help you:

HTML:

<div id="empty_div">
</div>

JS:

var documents = ['file_one', 'file_two', 'file_three'];

var tmpDocument, tmpAnchorTagPdf, tmpAnchorTagXls, parentContainer, i;

parentContainer = document.getElementById('empty_div');

for (i = 0; i < documents.length; i++) {
    tmpDocument = documents[i];

    tmpAnchorTagPdf = document.createElement('a');
    tmpAnchorTagPdf.href = 'your-domain.com/' + tmpDocument + '.pdf';
    tmpAnchorTagPdf.innerHTML = 'Start download ' + tmpDocument + '.pdf';

    tmpAnchorTagXls = document.createElement('a');
    tmpAnchorTagXls.href = 'your-domain.com/' + tmpDocument + '.xls';
    tmpAnchorTagXls.innerHTML = 'Start download ' + tmpDocument + '.xls';

    parentContainer.appendChild(tmpAnchorTagPdf);
    parentContainer.appendChild(tmpAnchorTagXls);
}

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Don't know of other questions but the below is how you create a dynamic link:

var link = document.createElement('a');
link.href = "href here";
// append it where you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.