abhishek
abhishek

Reputation: 149

How to get value of text box embeded in cell

I have the following code

<tr val='question'>
    <td>
        <input style='width: 500px' type='text' placeholder='Q.Enter your question here for radio button? '>
</tr>

How can i find the value of input box embedded in cell .

function saveUserDefQues(){

var table=document.getElementById("QuestionList");
var surveyquestionform=document.forms[0];
var count=$('#QuestionList tr').length
for (var i = 0; i<count; i++) {
var row = table.rows[i];
if(row.getAttribute('val')==='question')
    {
    var Cells = row.getElementsByTagName("td");;
    }

}       

}

Upvotes: 0

Views: 65

Answers (4)

Ritesh Kashyap
Ritesh Kashyap

Reputation: 394

See this Plunker

function getResult(){
    $( "tr" ).each(function( index ) {
  console.log($(this).find('input').attr('placeholder') );
});
}

Upvotes: 0

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18997

Since you are using Jquery this can be done this way.

replace this line of code

var Cells = row.getElementsByTagName("td");

with

var Cells = $(row).find('td');

var inputValue = Cell.find('input').val(); // gives you value of input


Code Refactoring recommended

I would like to refactor your code as below

HTML

<tr data-val='question'>     // use data-* attribute to add custom attributes into tags
    <td>
        <input style='width: 500px' type='text' placeholder='Q.Enter your question here for radio button? '>
    </td>                    // close your td
</tr>

Script

function saveUserDefQues(){      

 var surveyquestionform = document.forms[0];  // not sure what this is for, so ill leave it as is.

 $('#QuestionList tr[data-val="question"]').each(function(){ //loop all tr's  which has the data-val set to question     
   var inputValue = $(this).find('td input').val();          //get the value of input
   console.log(inputValue);            
 });  

}

Upvotes: 1

Salathiel Genese
Salathiel Genese

Reputation: 1909

document.querySelector('tr[val] > td > input').value;

Array.from(document.querySelectorAll('tr[val] > td > input')).forEach(function(entry, index, entries)
{
    entry.value; // you may store the value OR process with it AS you see fit
});

Upvotes: 1

ykaragol
ykaragol

Reputation: 6221

$("tr[val='question'] > td > input").val()

But first you need to write a valid HTML. </td> closing tag is missing. Also you need to put this tr in a <table>.

Upvotes: 0

Related Questions