I have simple table which looks like this:
<table>
<tbody>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="error">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</tbody>
</table>
When i click on tr i need to add custom class to it, but only whe i click on tr, not on the input inside it. How to do this?
I tried something like this:
table.find('tbody').on('click', 'tr:not(input)', function(){
jQuery(this).toggleClass('error');
});
table.find('tbody').on('click', 'tr', function(e) {
if (e.target != this)
return;
$(this).toggleClass('error');
});
I think the best you can do here is check the node name to see if it is the td or the tr - or include other tags if you want.
$('tbody').on('click', 'tr', function (e) {
if (e.target.nodeName.toLowerCase() === "td" || e.target.nodeName.toLowerCase() === "tr") {
$(this).toggleClass('error');
}
});
If you specifically want the input excluded, you could then exclude those:
$('tbody').on('click', 'tr', function (e) {
if (!$(e.target).is(':input')) {
$(this).toggleClass('error');
}
});
You can stop the event from bubbling up to the tr element by using event.stopPropagation:
table.find("tr").on("click", function () {
jQuery(this).toggleClass("error");
}).find("input").on("click", function (event) {
event.stopPropagation();
});
Here's some documentation for ya if you'd like to know more: http://api.jquery.com/event.stopPropagation/
Here's a JSFiddle of the solution: http://jsfiddle.net/tM59b/1/
Add a click listener to the input and return false; from it to stop the event from propagating to the tr.
Like so:
$('tr input').click(function(e){
return false;
});
Related
I am trying to clone a hidden table row, paste it and make it visible. For some reason the tr stays hidden. I've tried different ways but in the end I can not find a solution.
This is my code:
$(document).ready(function() {
$('#addproduct').click(function(e) {
e.preventDefault();
var item = document.getElementById('hiddenTemplate').cloneNode(true);
$('#hiddenTemplate').after(item);
$('#hiddenTemplate').css("visibility", visible);
});
});
#hiddenTemplate {
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<button id="addproduct" class="btn btn-primary">Add Product</button>
<table id="tableProducts">
<tbody>
<tr>
<td><b>SKU</b>:</td>
<td><b>Default Quantity</b>:</td>
<td><b>Position</b>:</td>
<td><b>Is Default?</b></td>
<td><b>User Defined?</b></td>
</tr>
<tr id="hiddenTemplate">
<td>
<input type="text" id="sku">
</td>
<td>
<input type="text" id="quantity">
</td>
<td>
<input type="text" id="position">
</td>
<td>
<input type="radio" id="isDefault">
</td>
<td>
<input type="checkbox" id="userdefined">
</td>
</tr>
</tbody>
</table>
Your code is missing quotes around visible:
$('#hiddenTemplate').css("visibility", "visible");
(and as said in the comments, use a class rather than an id)
There where 3 misstakes in your code
remove Attribute id
$('#hiddenTemplate').css("visibility", "visible");
see 2 and do it on $(item)
$( document ).ready(function()
{
$('#addproduct').click(function (e) {
e.preventDefault();
var item = document.getElementById('hiddenTemplate').cloneNode(true);
item.removeAttribute('id');
$('#hiddenTemplate').after(item);
$(item).css("visibility", "visible");
});
});
#hiddenTemplate
{
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="addproduct" class="btn btn-primary">Add Product</button>
<table id="tableProducts">
<tbody>
<tr>
<td><b>SKU</b>:</td>
<td><b>Default Quantity</b>:</td>
<td><b>Position</b>:</td>
<td><b>Is Default?</b></td>
<td><b>User Defined?</b></td>
</tr>
<tr id="hiddenTemplate">
<td>
<input type="text" id="sku">
</td>
<td>
<input type="text" id="quantity">
</td>
<td>
<input type="text" id="position">
</td>
<td>
<input type="radio" id="isDefault">
</td>
<td>
<input type="checkbox" id="userdefined">
</td>
</tr>
</tbody>
</table>
I could enable the textboxes as I click on the radio button using jquery. But I am not getting as to how to disable them if radio button is changed.
JSFiddle
http://jsfiddle.net/2LCnC/
HTML
<table>
<tr class="jsRadioMasterSlaveContainer">
<td>
<input type="radio" name="radioMultiSelectCheckbox" class="jsRadioMaster" value=""> Upto N random selection count
</td>
<td style="padding: 3px;">
<input type="text" placeholder="Count" id="" style="width:80px" class="jsRadioSlave" disabled/>
</td>
</tr>
<tr class="jsRadioMasterSlaveContainer">
<td>
<input type="radio" name="radioMultiSelectCheckbox" id="" value="" class="jsRadioMaster"> Fixed selection count
</td>
<td style="padding: 3px;">
<input type="text" name="" placeholder="Count" id="" style="width:80px" class="jsRadioSlave" disabled/>
</td>
</tr>
<tr class="jsRadioMasterSlaveContainer">
<td>
<input type="radio" name="radioMultiSelectCheckbox" id="" class="jsRadioMaster"> Fixed index selection count
</td>
<td style="padding: 3px;">
<input type="text" placeholder="Count" id="" style="width:80px" class="jsRadioSlave" disabled/>
</td>
</tr>
</table>
Jquery
$(".jsRadioMaster").each(function() {
$(this).click(function() {
HandleMasterRadioChange($(this));
});
});
function HandleMasterRadioChange(masterRadio) {
$(masterRadio).closest(".jsRadioMasterSlaveContainer").find(".jsRadioSlave").each(function() {
$(this).attr("disabled", !$(masterRadio).is(':checked'));
});
}
You can use prop(propertyName, function) and return boolen based on radio in same row
$(".jsRadioMaster").change(function() {
$('.jsRadioSlave').prop('disabled',function(){
return !$(this).closest('tr').find('.jsRadioMaster').prop('checked')
})
});
DEMO
I am trying to create a table that when a user click update, it will get the first and second input value. I am trying to use prev to find the first two DOM element and get the value, but fail, what should I do? Any help is appreciated.
$('.update_button').on("click",function(){
var update_val_1 = $(this).prev().find('input:first').val();
var update_val_2 = $(this).prev().find('input:nth-child(2)').val();
alert(update_val_1);
alert(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr class="gridtable">
<td><input type="text" value="apple" /></td>
<td><input type="text" value="one" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
<tr class="gridtable">
<td><input type="text" value="banana" /></td>
<td><input type="text" value="three" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
<tr class="gridtable">
<td><input type="text" value="berry" /></td>
<td><input type="text" value="ten" /></td>
<td><button type="button" class="update_button">UPDATE</button></td>
</tr>
The problem is that prev looks for sibling nodes. Your input elements are in different td's so they aren't siblings. Instead, you need to go to the parent row then grab the inputs from there.
Explicit example:
$('.update_button').on("click", function() {
var $parentRow = $(this).closest('.gridtable');
var $firstCell = $parentRow.find('td:first-child');
var $secondCell = $parentRow.find('td:nth-child(2)');
var $firstInput = $firstCell.find('input');
var $secondInput = $secondCell.find('input');
var update_val_1 = $firstInput.val();
var update_val_2 = $secondInput.val();
console.log(update_val_1);
console.log(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="gridtable">
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="one" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="banana" />
</td>
<td>
<input type="text" value="three" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="berry" />
</td>
<td>
<input type="text" value="ten" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
</table>
Simplified example:
$('.update_button').on("click", function() {
var $parentRow = $(this).closest('.gridtable');
var update_val_1 = $parentRow.find('td:first-child input').val();
var update_val_2 = $parentRow.find('td:nth-child(2) input').val();
console.log(update_val_1);
console.log(update_val_2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="gridtable">
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="one" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="banana" />
</td>
<td>
<input type="text" value="three" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
<tr class="gridtable">
<td>
<input type="text" value="berry" />
</td>
<td>
<input type="text" value="ten" />
</td>
<td>
<button type="button" class="update_button">UPDATE</button>
</td>
</tr>
</table>
I'm a big fan of the .closest(selector) and .find('selector') methods.
The first one goes up the dom until it finds the first match of the given selector.
The second one goes down the dom and finds all matches.
.eq(index) is like array[index] for jQuery. It takes a collection of jQuery elements and returns the one at the given index.
$('.update_button').on("click",function(){
var inputs = $(this).closest('.gridtable').find('input');
var value1 = inputs.eq(0).val();
var value2 = inputs.eq(1).val();
});
Please check my fiddle.
Fiddle
When i enter any data in any rows in slab_range, i need to autofill all the other rows of 'Slab Range' with a value 'No Bid'. If i left blank, nothing has to be filled. Likewise if i enter any data in 'Part Number', all the other rows of 'Part Number' has to be filled with value '2'. The rows are coming from db, so i cant tell how many rows it will be, it should iterate all the rows.
<tr>
<td>
<input size="1" id="sl[0]" name="sl[0]" value="1" type="text">
</td>
<td>
<input size="9" data-validation="required" name="slab_range[]" id="slab_range[]" type="text">
</td>
<td>
<input size="9" name="item_partno[]" id="item_partno[]" type="text">
</td>
</tr>
There you go, now it's your task to refactoring the code because both methods are equals.
var ProcessTable = (function () {
var _slabs, _partsNumber;
var _init = function () {
_slabs = $('input[name^="slab_range"]');
_partsNumber = $('input[name^="item_partno"]');
_slabs.on('blur', _slabBlurHandler);
_partsNumber.on('blur', _partNumberBlurHandler);
};
var _slabBlurHandler = function (e) {
var value = $.trim($(this).val());
if (value !== '') {
_slabs.val('No bid');
} else {
_slabs.val('');
}
$(this).val(value); // Because the previous line override the original value
};
var _partNumberBlurHandler = function (e) {
var value = $.trim($(this).val());
if (value !== '') {
_partsNumber.val('2');
} else {
_partsNumber.val('');
}
$(this).val(value); // Because the previous line override the original value
};
return {
init: _init
}
})();
ProcessTable.init();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="cart1" name="cart" method="post" class="single" action="price_edit_save.php?supplier_name=Jupiter+Microwave+Components+Inc&tender_id=151501">
<div class="clone_row">
<table style="border-collapse: collapse;" id="table" border="1" cellpadding="2" cellspacing="2" width="100%">
<thead>
<tr bgcolor="#E6E6FA">
<th width="4%">SlNo</th>
<th width="4%">Slab Range</th>
<th width="6%">Part Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input size="1" name="id[0]" value="9978" readonly="readonly" type="hidden">
<input size="1" id="sl[0]" name="sl[0]" value="1" type="text">
<input size="1" id="item_id[0]" name="item_id[0]" readonly="readonly" type="hidden">
</td>
<td>
<input size="9" data-validation="required" name="slab_range[]" id="slab_range[]" type="text">
</td>
<td>
<input size="9" name="item_partno[]" id="item_partno[]" type="text">
</td>
</tr>
<tr>
<td>
<input size="1" name="id[1]" value="9979" readonly="readonly" type="hidden">
<input size="1" id="sl[1]" name="sl[1]" value="2" type="text">
<input size="1" id="item_id[1]" name="item_id[1]" readonly="readonly" type="hidden">
</td>
<td>
<input size="9" data-validation="required" name="slab_range[]" id="slab_range[]" type="text">
</td>
<td>
<input size="9" name="item_partno[]" id="item_partno[]" type="text">
</td>
</tr>
<tr>
<td>
<input size="1" name="id[1]" value="9979" readonly="readonly" type="hidden">
<input size="1" id="sl[1]" name="sl[1]" value="2" type="text">
<input size="1" id="item_id[1]" name="item_id[1]" readonly="readonly" type="hidden">
</td>
<td>
<input size="9" data-validation="required" name="slab_range[]" id="slab_range[]" type="text">
</td>
<td>
<input size="9" name="item_partno[]" id="item_partno[]" type="text">
</td>
</tr>
</tbody>
</table>
</div>
<div class="addMoreDIV">
</div>
<table>
<tr>
<td>
<input value="--Update Data--" type="submit">
</td>
</tr>
</table>
</form>
And please, be more kindly when you ask for "help".
I have multiple input text inside a table. How can I automatically set value & focus the last input text once I'm done typing values from the 1st input text box?
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
my js code:
var d = id('my_table', document);
for (var x=1; x<d.rows.length; x++) {
d.rows[x].cells[12].firstChild.innerText = x;
d.rows[x].cells[12].firstChild.focus();
}
You could handle the blur event or the keypress event (to handle that the user pressed Enter) or a combination of both:
var inputs = document.querySelectorAll('#table input');
inputs[0].addEventListener('blur', setFocus);
inputs[0].addEventListener('keypress', function(e) {
if (e.keyCode === 13)
setFocus();
});
function setFocus() {
inputs[inputs.length - 1].value = '123';
inputs[inputs.length - 1].focus();
}
<table id="table">
<tr><td><input type="text" ></td></tr>
<tr><td><input type="text" ></td></tr>
<tr><td><input type="text" ></td></tr>
</table>
I am not sure, It the same as your want but its helps you, try this
<script src="http://code.jquery.com/jquery.min.js"></script>
<td> <input type="text" maxlength="4" tabindex="1" /> </td>
<td> <input type="text" maxlength="4" tabindex="3" /> </td>
<td> <input type="text" maxlength="4" tabindex="4"/> </td>
<td> <input type="text" maxlength="4" tabindex="2" /> </td>
<script type="text/javascript">
jQuery("input").on('input',function () {
if(jQuery(this).val().length == jQuery(this).attr('maxlength')) {
nextinput = parseInt(jQuery(this).attr('tabindex'))+1;
// Here you can take current input value and set where you want
jQuery("input[tabindex="+nextinput+"]").focus();
}
});
</script>