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 Answer 1

1

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];
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

$.pimp() {function();}

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.