0

I am developing a Chrome Extension that, when the user leaves the page, saves all the text from the textboxes on that page and outputs it to a file.

If I knew the IDs of the textboxes on the page, then it wouldn't be a problem, but the issue is that I need the extension to get the values of all of the textboxes on the page without knowing the IDs, as it will be a different website each time the extension is used.

Also, how would the information be collected? In a string? It would be nice to go down the page and add each textbox to a file, one by one, instead of one huge string.

I've never used jQuery or understood it, and there's probably a solution in it staring me in the face. If anyone suggests using it, please could you explain it a little bit?

Thanks in advance. I would post my code, but I don't know where to start - ergo I don't have any.

String
  • 155
  • 1
  • 5
  • 12

5 Answers5

3

you could store it in array using $.each, as :

var valsArr = [];
$("input[type=text]").each(function() {
    valsArr.push( $(this).val() );
});

or create object with name as key and value as its value, like:

var valsObj = {};
$("input[type=text]").each(function() {
    valsObj[this.name] = $(this).val();
});
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

You can do it like this:

function onClick(){
  var areas = document.getElementsByTagName("textarea");
  for(var i = 0; i < areas.length; i++){
    alert(areas[i].value);
  }
}
<textarea></textarea>
<textarea></textarea>
<button onclick="onClick()">Gather information</button>

Also see this regarding your "save to a file" question Using HTML5/Javascript to generate and save a file

Community
  • 1
  • 1
OddDev
  • 3,644
  • 5
  • 30
  • 53
  • This looks like it would work, but when I run the snippet I get `Uncaught ReferenceError: onClick is not defined`.... – String Apr 19 '15 at 17:26
  • @String I'm sorry, there was an formatting error :( I've edited my answer and it should work now. – OddDev Apr 20 '15 at 07:58
0

Make a for loop

for(i=0; i < $("input[type='text']").length; i++){
   $("input[type='text']").index(i).value();
}
Memolition
  • 492
  • 1
  • 5
  • 12
0

Use the selector and apply it in an each cycle.

$(":text").each(function(){
    alert($(this).val());
});
0

You can use .map() : It returns an array.

var arr = $(":text").map(function() {
    return this.value
}).get();  //add join(',') after get() to get a simple comma separated list.

Instead of input[type="text"] you could also use :text pseudo selector.

Demo

Shaunak D
  • 20,588
  • 10
  • 46
  • 79