Mohammed Sufian
Mohammed Sufian

Reputation: 1781

remove hidden field based on hidden field value using jquery

i would like to remove a hidden field from the form using jquery based on hidden field value.

please look the below html:

<input type="hidden" name="myvalue[]" value="value1"/>
<input type="hidden" name="myvalue[]" value="value2"/>
<input type="hidden" name="myvalue[]" value="value3"/>

i have jquery function which returns me the above value example:

$('#getValue').click(function(){

  .... processing code here.....

  return valueOfHiddenField;
});

now i would like to remove the hidden field from the form based on the value return by the getValue

so if the method returns me value1 then any input hidden field in the form with value as value1 should be removed. in this case the first input field of my html above.

any help or suggestion would be a great help ... thanks in advance

Upvotes: 2

Views: 11321

Answers (4)

Biswa
Biswa

Reputation: 520

You can also do like this:

let hiddenValue = $(this).val();
$('input[type="hidden"][value="'+hiddenValue+'"]').remove();

Get the value of a hidden field and then use it dynamically and remove specific <input type="hidden">

Upvotes: 0

Sid
Sid

Reputation: 801

$('#getValue').click(function () {
    $(document).find("input[type=hidden]").each(function () {
        if ($(this).value == 'Your Value') {
            $(this).remove();
        }
    });
    return valueOfHiddenField;
});

Upvotes: 0

Rick Lancee
Rick Lancee

Reputation: 1659

$('input[type="hidden"][value="+YOUR_VALUEVAR+"]').remove();

fiddle: http://jsfiddle.net/9tsgy/

Upvotes: 10

Deepak Ingole
Deepak Ingole

Reputation: 15742

You can iterate over input:hidden elements to check values and remove() based on value,

$("input:hidden").each(function () {
    if (this.value == "value1") { // your condition here
        $(this).remove()
    }
}

Upvotes: 2

Related Questions