0

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"/>

3
  • jQuery.closest does not work as you expected. It doesn't search through element siblings. Commented Aug 7, 2017 at 8:40
  • You better use the jQuery.next() function to get the next input. Commented Aug 7, 2017 at 8:41
  • Explain me the output that you want to see Commented Aug 7, 2017 at 8:46

3 Answers 3

1

This is what you want to do in the clearest way ^^

$(document).ready(function(){
   $(".PageNumber").each(function(index, element) {
    var pagenum = $(this).next('.page').val();  
    $(this).attr("data-page", 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"/>

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

1 Comment

np, i like to help
0

jQuery.closest() is not what you want. this function will find the closest parent element that matches the selector, starting with the current element. https://api.jquery.com/closest/

You better use the jQuery.next() function to get the next input. https://api.jquery.com/next/

$(document).ready(function() {
  $(".PageNumber").each(function(index, element) {
    var pagenum = $(this).next('.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" />

2 Comments

how can i set the value to data-page too?
jQuery.attr() is used to get and set attributes. jQuery.attr('data-page') will get the attribute and jQuery.attr('data-page', value) will set the attribute. You can also use the jQuery.data() function. api.jquery.com/data
0

The jQuery closest function doesn't do what you need it to do. Here is its description.

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

You could use next to get the input, here is an example:

$(document).ready(function(){
   $(".PageNumber").each(function(index, element) {
    var pagenum = $(this).next('.page').val();
    $(this).data("page", 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"/>

3 Comments

how can i set the value to data-page too?
your editet answer is not replace with value of input for each data-href
@Inaz I'm not sure what you mean, if you want to change the a element's data-href property you can do the same thing as I've done for the data-page property.

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.