1

I have list of items with the same class name - "fileLink"

When i submit the form below I would like to send, via AJAX, the list of file URL to the AJAX file (and the email address that user type).

How do i send array by class name to ajax file?

<ul class="feeds" id="filesList">

    <li>
        <a href="http://file_folder/3604.jpg" target="_blank" class="fileLink">356458.e109abcc152f2536b969b32f6e8439e5</a>
        <span class="text-muted">DOC</span>
    </li>                                       
    <li>
        <a href="http://file_folder/3605.jpg" target="_blank" class="fileLink">357161.e4948db1d64c88b9396647aaa89a7f10</a>
        <span class="text-muted">jpg</span>
    </li>                                       
    <li>
        <a href="http://file_folder/3606.jpg" target="_blank" class="fileLink">357213.e109abcc152f2536b969b32f6e8439e5</a>
        <span class="text-muted">jpg</span>
    </li>   


   <form class="form-inline m-t-20" action="#" id="#emailFile-form">

       <a href="#" type="submit" class="btn btn-default" id="emailFile_btn">SEND</a>
   </form>

</ul>
Roi
  • 109
  • 9

1 Answers1

2

I think his fiddle will get you started: jsfiddle

First, you have to correct the html code for your form:

<!-- not correct: id="#emailFile-form" -->
<form class="form-inline m-t-20" action="#" id="emailFile-form">
  <!-- use input or button to send form, not a -->
  <button href="#" type="submit" class="btn btn-default" id="emailFile_btn">SEND</button>
</form>

Then, you need a JS function that listens when the form is being sent, and then grab the links with the specified classname:

// get form element
var form = document.getElementById("emailFile-form");
// form controller
form.onsubmit = function(e){
  // stop form from sending
  e.preventDefault();
  var list = [];
  // grab all the links with class .fileLink
  var links = document.getElementsByClassName("fileLink");
  // put all links in the: list
  for (var i=0; i<links.length; i++)
    list.push( links[i].href );
  // send the list by ajax function
  ajaxSend(list);   
};

Also, don't forget you need to define a function that does the actual ajax work:

function ajaxSend(data) {
    /** insert ajax code here **/
  alert("links: "+data.join(","));
}

Learn about sending data through ajax here:

jQuery.ajax() documentation

or here:

How to make an AJAX call without jQuery?

verjas
  • 1,638
  • 1
  • 10
  • 17