1

I want to iterate through the table and save input values to the array, but I get js error:

Object doesn't support property of method 'find'

Here is my code:

function Elem(tex, check, r, c)
{
    this.text = tex;
    this.checkbox = check;
    this.row = r;
    this.column = c;
}

var Elems = [];

function fill()
{
    var table = document.getElementById("tab");
    for(var i=table.rows.length-1;  i>=2; --i)
    {
        for(var j=table.rows[i].cells.length-1; j>=1 ; j=j-2)
        {
            var check = $(table.rows[i].cells[j-1]).find('input'); 
            check = check.is(":checked");
            var ans = $(table.rows[i].cells[j]).find('input'); 
            ans = ans.val();
            var ans = new Elem(ans, check, i, j/2);
            Elems.push(ans);
        }
    }
}

And here is how my table looks:

...
<table id="tab">
<tr>
    <td rowspan="2">
        <strong>delete row</strong>
    </td>
    <td>
        <strong>checkbox info</strong>
    </td>
    <td>
        <strong>description</strong>
    </td>
</tr>
<tr>
    <td>
        <strong>delete column</strong>
    </td>
    <td>
         <input type='checkbox' name='delColumn_1'/>
    </td>
</tr>
<tr>
    <td>
        <input type='checkbox' name="delRow_1"/>
    </td>
    <td>
        <input type='checkbox' class='check' name="ele.check"/>
    </td>
    <td>
        <input type='text' name="ele.text" required="required"/>
    </td>
</tr>
</table>
...

And I want to get text value from ele.text and checkbox value from ele.check. I will be very grateful for help.

1
  • 1
    .find() is a jQuery method, meaning that you cannot use it without including the jQuery library. It's not a native DOM method. Commented Aug 26, 2014 at 13:27

2 Answers 2

2

At best guess, it appears you are mixing traditional JavaScript and jQuery without understanding the connection.

Take the line:

var check = table.rows[i].cells[j-1].find('input'); //err

If you simply break this apart, piece by piece, you will eventually find the problem.

var check = table.rows[i]

Then, if returns a non-null value, add to it:

var check = table.rows[i].cells[j-1]

Than should also be non-null, so add the last bit to it.

var check = table.rows[i].cells[j-1].find('input'); //err

And that should error out, as there is no find method on a td element.

However, find is a well-known jQuery method so it is likely that you meant to

  • Include a reference to jQuery in your code
  • Change it to jQuery syntax
  • var check = $(table.rows[i].cells[j-1]).find('input');

Or, you could also look at using

table.rows[i].cells[j-1].querySelector("input");

Which I haven't tested, but should do what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I mess a little bit, I use in other function jquery because I don't need to have index, and forget that find is a jQuery method. Thanks for detailed explanation! Also querySelector of course works ;)
2

Find() is a method that is provided on elements by jQuery. If you want to use it, you have to first access the jQuery object.

$("#tab tr td").each(function() {
     var ans= $(this).find("input").val();
     // do stuff here
});

Small jsFiddle. You can find more details about the find method in the jQuery docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.