tags:

views:

606

answers:

1

I have the following

$().ready(function() {
    $("input[name^=totalRent_]").each(function()
    {    var input = $(this);  
         var name = input.attr('name');  
         var num = /\d+$/.exec(name)[0];

My html form has:

    <input type="text" name="totalRent_1"  value="" /> <br />
    <input type="hidden" name="totalRent_1_hidden" /><br />
    <hr />
    <input type="text" name="totalRent_2"  value="" /> <br />
    <input type="hidden" name="totalRent_2_hidden" /><br />
    <hr />
    <input type="text" name="totalRent_3"  value="" /> <br />
    <input type="hidden" name="totalRent_3_hidden" /><br />
    <hr />

Now, I get a javascript error saying: /\d+$/.exec(name) is null

The each function is attached to ("input[name^=totalRent_]"). What do I need to do so that in's only attached to total_rent and not with totalRent_..._hidden ?

Thanks in advance.

+1  A: 

If you only want to select the text inputs and not the hidden ones you change your selector to filter by multiple attributes (name and type):

$('input[name^=totalRent_][type=text]')

Or you can check the type attribute of the element on the each callback function:

$('input[name^=totalRent_]').each(function(){
  var input = $(this);  
  if (input.attr('type') == 'text'){
    var name = input.attr('name');  
    var num = /\d+$/.exec(name)[0];
  }
});
CMS
wow, that's p.i.m.p.
FALCONSEYE