i have many a href tags and also i have input with type hidden. i wrote a code to get the value of closest input to each a href then put the value of that input instead of data-page and the text of the a tag. i wrote the below code but it is not work.
$(document).ready(function(){
$(".PageNumber").each(function(index, element) {
var pagenum = $(this).closest('.page').val();
var linkk = $(this).attr("data-page");
var linktext = $(this).text();
$(this).attr(pagenum);
$(this).text(pagenum);
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<a class="PageNumber" data-page="" data-href="home.htm">1</a>
<input type="hidden" value="1" class="page"/>
<a class="PageNumber" data-page="" data-href="home2.htm">1</a>
<input type="hidden" value="2" class="page"/>
<a class="PageNumber" data-page="" data-href="home3.htm">1</a>
<input type="hidden" value="3" class="page"/>
jQuery.closestdoes not work as you expected. It doesn't search through element siblings.jQuery.next()function to get the next input.