javascript - How to add and remove attribute in HTML using jquery onchange event? -
hello guys want ask how can create jquery can detect user input , assign attribute or remove attribute based on user input. scenario have table 4 columns. first dropdown. , rest textboxes readonly. default option dropdown null or empty. if user choose dropdown, 2 texboxes should not readonly. on process don't have error if user set again null. 2 textboxes won't go readonly again. don't know where's error.
here's jquery code:
$("[id^=code]").on('change',function(){ var index = this.id.match(/\d+/)[0]; var validate = $('#code'+index).val(); if(validate != ''){ $("#qty"+index).removeattr('readonly'); $("#price"+index).removeattr('readonly'); } }); $("[id^=code]").on('change',function(){ var index = this.id.match(/\d+/)[0]; var validate = $('#code'+index).val(); if(validate == ''){ $("#qty"+index).attr('readonly'); $("#price"+index).attr('readonly'); } });
and table
for($i = 1; $i < 16; $i++){ echo "<!-- item {$i} -->"; echo "<tr>"; echo "<td>"; echo "<select name='code[]' id='code{$i}' style='width:100'>"; echo "<option value=''><label>--choose items--</label></option>"; foreach($resultgetcode->result_array() $list){ echo "<option value='".$list['itemid']."'>".$list['itemcode']." --- ".$list['itemname']."</option>"; } echo "</select>"; echo "</td>"; //echo "<td><input type='text' name='item[]' class='k-textbox' id='item{$i}' value='' /></td>"; echo "<td><input type='text' name='qty[]' id='qty{$i}' style='text-align: center' readonly='readonly' /></td>"; echo "<td><input type='text' name='price[]' id='price{$i}' style='text-align: right;' onblur='' readonly='readonly' /></td>"; echo "<td><input type='text' name='total[]' id='total{$i}' style='font-family: courier; text-align: right; background-color: lightgray; color: red' readonly='readonly' value='' /></td>"; echo "<tr>"; }
here's fiddle: http://jsfiddle.net/rochellecanale/jnkc3/5/
would easier if create fiddle, try this
if(validate == ''){ $("#qty"+index).prop('readonly', true); $("#price"+index).prop('readonly', true); }
update. fiddle
Comments
Post a Comment