santa
santa

Reputation: 12512

Getting all links on the page into form fields w\jQuery

I am working on an app that helps with Information Architecture. I am at the point where I need to get the links on a page entered into form fields. Is there a way to point the page URL and have all links intered into the text fields?

I think jQuery should have enough power to do this type of work... Perhaps I need to look for $('a') and end everything withing tags? I need the link text only, not the URL itself. Not really sure how, but I feel it should be possible.

Upvotes: 0

Views: 314

Answers (1)

thastark
thastark

Reputation: 1253

The below script, on DOM load, will create one text input control containing the link text of each anchor tag on the page (just copy paste into your page):

<script type='text/javascript'>
 $(function() {

   var i = 0, 
       inputs = "<input type='text' value='' />";

    // loop through each a tag and grab the link text
    $("a").each(function() {
      i++;

      // create a new text field and append to outFields
      // feel free to change the input tag template above
      $(inputs)
         .attr("id", "field" + i)
         .attr("name", "field" + i)
         .value($(this).text())
         .appendTo("#outFields");

    });

 });
</script>

<div id="outFields"></div>

Upvotes: 1

Related Questions