patternjavascriptMinor
Copying and pasting one inputs value into other input with the same name
Viewed 0 times
inputsthesamewithintovalueinputonecopyingname
Problem
I am trying to write something that will copy the current `
This is the code I've written:
A fiddle to make everyones life easier: http://jsfiddle.net/6jGLD/
I feel like there are too many selectors being called upon and the code is somewhat difficult to read.
s value and enter it into any that start with the same name.
names will follow this pattern: price-0, price-1, price-2, upc-0, upc-1, upc-2.
So if a user enters a value in and hits copy the value should be transferred over to all input whos name start with price`This is the code I've written:
$(document).on('click', '.--copy', function () {
var input_name = $(this).closest('div').find('input').attr('name').split('-')[0];
$('input[name^=' + input_name + ']').val($(this).closest('div').find('input').val());
});A fiddle to make everyones life easier: http://jsfiddle.net/6jGLD/
I feel like there are too many selectors being called upon and the code is somewhat difficult to read.
Solution
Why not at least save the intermediate to avoid rerunning that:
$(document).on('click', '.--copy', function () {
var obj = $(this).closest('div').find('input');
var input_name = obj.attr('name').split('-')[0];
$('input[name^=' + input_name + ']').val(obj.val());
});Code Snippets
$(document).on('click', '.--copy', function () {
var obj = $(this).closest('div').find('input');
var input_name = obj.attr('name').split('-')[0];
$('input[name^=' + input_name + ']').val(obj.val());
});Context
StackExchange Code Review Q#33213, answer score: 2
Revisions (0)
No revisions yet.