2

I've got a page with a handful of input fields.

I need to find the fields with an array of values, and if so, .remove() the closest('tr')

The markup is similar to this

<table>
  <tr>
    <td>
      <input type="text" value="this">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" value="that">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" value="them">
    </td>
  </tr>
</table>

I need to find "this" and "that", and if they are there, remove their <tr> container (and themselves) so I'd end up with:

<table>
  <tr>
    <td>
      <input type="text" value="them">
    </td>
  </tr>
</table>

I've tried this:

jQuery(document).ready(function($){
    var badfields = ['this', 'that'];
    var fieldvalue = $('input[type="text"]').val();

    if($.inArray(fieldvalue, badfields) > -1){
        $(this).closest('tr').remove();   
    }
});

but it doesn't seem to want to work?

3 Answers 3

2

You need to iterate over all the fields using .each, so something like this:

$('input[type="text"]').each(function() {
    var fieldvalue = $(this).val();

    if ($.inArray(fieldvalue, badfields) > -1) {
        $(this).closest('tr').remove();   
    }
});

Example: jsfiddle

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

3 Comments

why not use .parent("tr") instead of .closest? .parent conveys greater intention IMO
@JoshE Because I tried to edit the OP's code as little as possible to make it work. You're correct, .parent does convey greater intention, but I'd rather not confuse anybody by changing too much at once.
I ended up using .parent() - for some reason I though only closest could traverse up more than 1 element. Other than that, it works beautifully! Thanks :)
1

You can be very concise sometimes with jQuery. jQuery has content selectors you can use for this type of purpose:

$("input[type=text][value=this], [value=that]").parents("tr").remove();

since you don't necessarily know this or that beforehand, you can do something like this:

var badfields = ['this', 'that'];
$(badfields).each(function(i) { 
    $("input[type=text][value=" + this + "]").parents("tr").remove();
});

1 Comment

This works if you know the values beforehand, or want to generate the selector string yourself, but isn't so great for more general solutions...
0

You can use each to iterate through the selector. this in your inArray scope is not the element you were looking for.

DEMO: http://jsfiddle.net/

html:

<table>
 <tr>
  <td>
  <input type="text" value="this">
  </td>
 </tr>
 <tr>
  <td>
   <input type="text" value="that">
  </td>
 </tr>
 <tr>
  <td>
   <input type="text" value="them">
  </td>
 </tr>
</table>

js:

jQuery(document).ready(function($){
 var badfields = ['this', 'that'];
    $('input[type="text"]').each(function(){
        if( $.inArray(this.value, badfields) > -1 ){
            $(this).closest('tr').remove();
        }
    });
});

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.