0

So basically I have some elements with some data-values in a file . I want to dynamically create links to them using those data-values to type the text, href etc.

For example, I have two divs, id'ed firstand second. They have importance values, respectively "most important" and "least important".

I want to create two links to them, with the phrase "This is a link to the most/least important div" using JavaScript/jQuery. Below is a non-working example.

File a.html (ommiting headings and such, ofc):

<div id='first' data-importance='most important'>This is the first div.</div>

<div id='second' data-importance='least important'>This is the second div.</div>

File b.html:

<p><a class='makelink' data-linkto='first'></a></p>

<p><a class='makelink' data-linkto='second'></a></p>

$(`.makelink`).each( function(){
  var target = $(this).data('linkto');
  $(this).attr('href','b.html#' + target);
  var imp = $('b.html #' + target).data('importance'); 
  $(this).html('Link to the ' + imp + ' div');
});

However nothing happens. Any help is appreciated.

2
  • 1
    did you notice the error message in the browser console? Commented Apr 5, 2019 at 0:29
  • 1
    You can't alter the DOM of another file unless that file is open in a window or frame that the current file knows about. It's not clear what you're looking for. Commented Apr 5, 2019 at 12:24

1 Answer 1

1

Your code seems correct except the selector should be inside quote and the string generated in the html() is not properly formatted:

$('.makelink').each( function(){ // Enclosed the selector with single/double qoute
  var target = $(this).data('linkto');
  $(this).attr('href','#' + target);
  var imp = $('#' + target).data('importance'); 
  $(this).html('Link to the '+ imp + ' div'); // concatenate the string with the variable using +
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id='first' data-importance='most important'>This is the first div.</div>

<div id='second' data-importance='least important'>This is the second div.</div>

<p><a class='makelink' data-linkto='first'></a></p>

<p><a class='makelink' data-linkto='second'></a></p>

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

7 Comments

Thanks for noticing those mistakes. The code you provided works as-is. However I tried to do the same implementation for getting data-values in elements in another file and it does not work (I updated the question). Would you know why? Thanks
You conveniently ignored the part of the question about "another file".
@HereticMonkey No. I had phrased the question ommiting that part because I thought any solution for elements in the same file would work, and Mamum's answer works in that case. However my problem was not really solved...
@Questioner my comment was directed to Mamun, not you. I'm not sure why you accepted this answer if it didn't answer your question...
@HereticMonkey I understand that. I was just explaining that his answer was good for the question as previously asked. So it is my fault that his answer does not apply anymore.
|

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.