change value of name attribute using jquery

HirenMangukiya

I print data in table and try to change name of specific hidden field but i cant my code is as below

        <table class="table table-hover">
            <thead>
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>sr no</th>
                <th>seller</th>
                <th>shape</th>
                <th>size</th>
                <th>color</th>
                <th>discount</th>
                <th>client</th>
            </tr>
            </thead>
            <tbody>
            @foreach($cod_details as $cod)
                <tr>
                    <td>
                        <input type="checkbox" name="ids[]" id="{{ $cod->id }}" value="{{ $cod->id }}">
                        <input type="hidden" value="{{ $cod->report_no }}" id="{{ $cod->report_no }}" name="" class="report">
                    </td>
                    <td>{{ $cod->id }}</td>
                    <td>{{ $cod->seller }}</td>
                    <td>{{ $cod->shape }}</td>
                    <td>{{ $cod->size }}</td>
                    <td>{{ $cod->color }}</t
                    <td>{{ $cod->discount }}</td>
                    <td>{{ $cod->client }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>

this shows all details in table and working perfect.

$(document).ready(function(e){
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                $(this).closest('input[type="hidden"]').attr('name','report_no[]');

            }
            else{

            }
        });
    });

I try to add name = report_no[] on checked of checkbox but i cant.

T.J. Crowder

It's the very next element in the DOM, immediately after the checkbox. So in your handler:

var input = $(this).next();

...will give you a jQuery wrapper for that input field.

Side note: if($(this).prop("checked") == true){ is a very long-winded way to write if (this.checked), I'd just use that. :-)

So:

$('input[type="checkbox"]').click(function() {
    if (this.checked) {
        $(this).next().attr('name','report_no[]');
    }
    else {

    }
});

If you want to clear it when the checkbox is unchecked, then:

$('input[type="checkbox"]').click(function() {
    $(this).next().attr("name", this.checked ? "report_no[]" : "");
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jquery issue in getting value by name attribute

How to change name attribute after clone using jQuery?

jQuery - How to select value by attribute name starts with

How can I change the input element name attribute value in a razor view model using a custom attribute in a model?

Disable a textarea field with name attribute using jquery

Change element name based on attribute value XSLT

Replace the attribute rel with class using jQuery - change also the value

Change value of name attribute on root xsl

Change an attribute value using XSL

attribute name get value with jQuery

How to empty the name attribute in form in jquery using source object on change

jquery change class if attribute value =

JQuery get attribute value with same name

jQuery change number inside a name attribute

Returning the value of an attribute using jquery

Change href attribute using jquery

getting an attribute value using jquery

Change value of class attribute in html using jquery or javascript

Change tag name based on attribute using jquery and textarea

find lowest value by using item name and change low value background color in html table using jquery

Change img src attribute value in loop - jquery

Change TwitterButton attribute value with JQuery

How to get "ID" value from "name[ID][OTHER]" in attribute of element using jquery?

Change value attribute of element with specific name attribute

(Struct *) Initialization using attribute value name?

change event using name attribute of checkbox JS

How to change the name of a select tag attribute with jquery

Change the value of span using Javascript or Jquery using <input> or text attribute

jquery change style class attribute value