Mark Sandman
Mark Sandman

Reputation: 3333

JQuery finding a relative form element in the same row

You may have seen my last question where I use the following to inject a row into a table

$(':radio').live("click", function() {     
  $('#bdTable tr:last').after(makeHTML()); 
 });

the makeHTML() function spits out the following code

<tr>
    <tr>
        <td>
            <input type="amount" name="amount" id="amount" >        
        </td>
        <td>                    
        <select name="frequency" id="frequency" >
            <option value="">Weekly</option>
            <option value="">Fortnightly</option>
            <option selected="selected" value="">Monthly</option>
            <option value="">Annually</option
        </select>
    </td>
    <td >                       
        <input id="Yes" name="Yes" value="0" type="radio">
        <label for="">Yes</label>
        <input id="No" name="No" value="1" type="radio" >
        <label for="">No</label>
    </tr>
</tr>

now is there a way I can add a condition that the click function will only be activated if the text box in the same row as the radio button has a length greater than zero (so something has been entered). That way a new row is only injected if that row is complete. Sorry if my wording is awful, I can expand on this.

Upvotes: 0

Views: 1144

Answers (3)

Chandu
Chandu

Reputation: 82943

Note: You are using same id(amount) for multiple HTML elements. It is invalid. Also there is no type called amount for an input element. Change it to type="text"

Try this:

$(':radio').live("click", function() {        
    var amount = $.trim($(this).closest("tr").find("input[name='amount']").val());
    if(amount.length > 0){
        $('#bdTable tr:last').after(makeHTML());   
    }
});

Upvotes: 1

user750158
user750158

Reputation: 163

Not sure i understand the question but try:

var myLength = $("#myTextbox").val().length;
    if(myLength > 0)
    {
          // work your magic
    }

Try it!

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this

$(':radio').live("click", function() {     
  var $amount = $(this).closest("tr").find("#amount").val();
    if($amount.length > 0){
        $('#bdTable tr:last').after(makeHTML());   
    }
 });

Upvotes: 0

Related Questions