Ramy Ayman
Ramy Ayman

Reputation: 61

I want to append a value on an existing value

I have two input fields and their names are "From_1" and "To_1" , i want when i check the checkbox the names will be "From_1_Dis" and "To_1_Dis" and when i uncheck they return again to "From_1" and "To_1"

my html code :

<div class="col-md-11" id="worktimeID">
<input type="text" name="From_1" class="time" placeholder="00:00" />
<input type="text" name="To_1" class="time" placeholder="00:00" />
 <input type="checkbox" id="checked" /><label>Day Off ?</label>
</div>

my jquery code :

$(this).closest("div").find(':text').prop('name',val().append('_Dis'),this.checked);

Upvotes: 0

Views: 179

Answers (1)

Barmar
Barmar

Reputation: 781088

.append() is for appending to a DOM element. String concatenation is done with the ordinary JavaScript + operator.

val() is for getting the value of an input, not the old value of a property. And if you want to get information from the element that you're calling .prop() on, you need to pass a function to it.

$("#checked").click(function() {
    var checked = this.checked;
    $(this).closest("div").find(":text").prop('name', function() {
        if (checked) {
            return this.name + "_Dis";
        } else {
            return this.name.replace("_Dis", "");
        }
    });
});

Upvotes: 1

Related Questions