Home > Mobile >  How do I adapt this .prop to return mutiple href?
How do I adapt this .prop to return mutiple href?

Time:10-21

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);

CodePudding user response:

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  href="https://example.com/?one">one</a>
<a  href="https://example.com/?two">two</a>
<a  href="https://example.com/?three">three</a>
<hr>
<input id="form-field-name" size="60">

CodePudding user response:

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)
  • Related