0

I Have created href tag passing a pdf file and i'm getting that href value in javascript and i have to pass that javascript variable to html tag how it is possible , it is possible means please help.

Here my html code:

 <div class="pdfdwnld">
    <a href="C:/xampp/www/htdocs/rajashri/uploads/cata111log.pdf" download  class="pdfbtn" data-toggle="modal" data-target="#myModal">Download Pdf</a>
 </div>

Here my Javascript code:

<script type="text/javascript">
var linksArray = document.getElementsByClassName("pdfbtn");
var myFunction = function(event) {
    event.preventDefault();
    var href = this.getAttribute("href");
    alert('hello ' + href);
    return false;
};
for (var i = 0; i < linksArray.length; i++) {
    linksArray[i].addEventListener('click', myFunction, false);
}

In html tag this i need pass javascript variable for like data-value="" here only:

<div class="form_desc mob_view">
    <a class="fancybox" data-fancybox-group="iframe" data-value="" href="#pop_form">DOWNLOAD PDF</a> 
</div>
Hari
  • 21
  • 1
  • 7
  • did u mean this? https://stackoverflow.com/questions/4836290/how-to-change-html-object-element-data-attribute-value-in-javascript – Kasnady Feb 23 '18 at 04:26
  • Yaa i have javascript variable but how to pass that to data-value=" ". – Hari Feb 23 '18 at 04:29

3 Answers3

0

You can set values using jQuery

$('#pop_form').data('myval',20); 

It will set value to data-myval as 20

myval can be any name you can use.

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
0

Try this,

 var allElements = document.getElementsByClassName('fancybox');
 var firstElement = allElements[0];
 firstElement.dataset.value = href;
imanshu15
  • 734
  • 3
  • 21
0

If you need to set/modify value of data-value attribute

function(event) {

  event.preventDefault();

  // access element which fired event by > event.target
  var href = event.target.getAttribute('href');

  // Set attribute
  document.getElementsByClassName('fancybox')[0].setAttribute('data-value', href)

}
manjeet
  • 1,497
  • 10
  • 15