0

For each input field on the page, that has an ID ending in "_name" and "_value", I've placed in two arrays. And for each of the values of the input (there are equal number of inputs for the nameArr and valArr) in the name array I would like to place before the input of the namrArr as a heading, then hide the input. However, I cannot seem to access to the value of the input once it's placed in an array??

var name = $("[id$=_name]");
var nameArr = $.makeArray(name);
var val = $("[id$=_value]");
var valArr = $.makeArray(val);

for(var i = 0; i < valArr.length; i++){
    $(nameArr[i]).before("<h3>"+nameArr[i].val()+"</h3>");
    $(nameArr[i]).hide();
}
1
  • post html or make fiddle Commented Jul 25, 2013 at 10:54

3 Answers 3

1

You could simplify it, as your jQuery selector is already returning an array:

$("[id$=_name]").each(function (index, item) {
    $(this).before("<h3>" + $(this).val() + "</h3>");
    $(this).hide();
});
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to make it an array, it already is.

for(var i = 0; i < val.length; i++){
    $(name[i]).before("<h3>"+$(name[i]).val()+"</h3>");
    $(name[i]).hide();
}

1 Comment

Ahh, no array. Even better! Thanks! Although it doesn't allow the "val()" method!
0

use map()..

  var valuesArray=$("[id$=_name]").map(function(){
      return this.value;
 }).get();

  var namesArray=$("[id$=_value]");


 for(var i = 0; i < namesArray.length; i++){
   $(namesArray[i]).before("<h3>"+valuesArray[i]+"</h3>");
   $(namesArray[i]).hide();
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.