skip to Main Content

How do I adapt this jQuery .prop() to return mutiple href results?

window.onload = function() {
  var val = jQuery(".ro_title").prop("href");
  jQuery("#form-field-name").val(val);

2

Answers


  1. With a each loop. Through each element containing the .ro_title CSS class, concat the href property to the val variable like this:

    var val = '';
    
    jQuery(".ro_title").each(function(index) {
         val += jQuery(this).prop("href") + ', ';
    });
    
    jQuery("#form-field-name").val(val)
    
    Login or Signup to reply.
  2. You are probably looking for a combination of .map() and .toArray():

    window.onload = function() {
      var val = jQuery(".ro_title").map(function () { return jQuery(this).prop("href"); }).toArray();
      jQuery("#form-field-name").val(val);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <a class="ro_title" href="https://example.com/?one">one</a>
    <a class="ro_title" href="https://example.com/?two">two</a>
    <a class="ro_title" href="https://example.com/?three">three</a>
    <hr>
    <input id="form-field-name" size="60">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search