5

I have multiple input fields on a form like below:

<div class="ginput_container ginput_container_product_calculation">
<input type="hidden" class="gform_hidden" value="Space Cost:" name="input_97.1">
<input type="hidden" class="gform_hidden" value="$150.00" name="input_97.2">
<input type="hidden" class="ginput_quantity_1_97 gform_hidden" value="1" name="input_97.3">
</div>

I need to prevent all hidden input fields in the form from being submitted. The best solution I found so far is to remove the name attributes of input fields but I've no idea how it can be done using jQuery. Any help would be appreciated!

1
  • Google for jquery removeAttr - you will understand it in detail Commented Apr 14, 2016 at 10:17

4 Answers 4

10

Use .removeAttr().

$("input[type='hidden']").removeAttr('name');

OR

$(":hidden").removeAttr('name');

OR

$("input:hidden").removeAttr('name');

Edit :- Another way to disallow some inputs to post is to make them disabled as disabled form inputs are not submitted.

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

5 Comments

Is there a way to prevent submission of all the fields inside div?
yes..you can add disable attribute to all the fields inside the div..the disabled fields can't be submitted.
@Alex..use $(":hidden").attr('disabled','disabled')
You sure disable attribute can be added to all the fields inside a div? Including <p>, <span>, <label> etc? I think it works for input only.
@Alex...it doesn't make sense disabling span or p because these will not be submit with your form..only form inputs will be submitted.
0

Try this

$('input[type=hidden]').removeAttr('name')

Comments

0

Use removeAttr() as:

$(function() {
    $('input[type=hidden]').removeAttr('name');
});

Comments

0

you can remove the name attribute as is previously mentioned - but in my opinion this will lead to potential problems down the line - better to simply set the disabled attribute to "true". This works because disabled form inputs are not submitted with the rest of the form and is a cleaner way of removing that input value than removing the name and then potentially forgetting to set it later.

the disabled form inputs can be returned to active duty by setting the disabled attribute to false when required.

2 Comments

So how can I set the disabled attribute using jquery? I've no access to html code so i can't edit it.
$('input[type=hidden]').attr('disabled',true) should do it - same concept as modifying the name attribute - but less likely to cause issues with other code / functions

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.