Reputation: 11300
I am trying to change the values of some input element when a value of select dropdown changes.
When the select dropdown changes, i want to change some hidden textbox and a normal textbox which are with a td with class="owner"
.
Within the td
tag there are multiple elements with type="hidden"
and one input type= text. I want to change the values of element with type="hidden"
and id
ending with _lkid
and _lkold
and then the only element within the td
which is of type="text"
and is within a span
tag
I don't have an id
of the elements within the td
as they are generated automatically nor can i assign a class to those elements. i can only assign class to the td
.
I am able to change the values of hidden text, but i am unsure of how to change the type="text"
field. Any pointers would be helpful.
Jsfiddle : https://jsfiddle.net/4honph7n/3/
This js is probably not the best/effective code written, taking some baby steps with jquery
HTML
<select class="selectClass">
<option>Option 1</option>
<option>Option 2</option>
</select>
<table>
<tr>
<td class="owner">
<select>
<option>1</option>
<option>2</option>
</select>
<input type="hidden" id = "someid_lkid" value="00590000002BIF7">
<input type="hidden" id = "someid_lkold" value="Some Text">
<!-- there are some more hidden fields -->
<span class="lookupInput">
<input type="text" id = "someid" value="Some Text">
</span>
</td>
</tr>
</table>
Js
$(document).ready(function () {
$('.selectClass').change(function() {
$('.owner').each(function(i) {
$(this).find("input[type='hidden']").each(function(){
if($(this).attr('id').indexOf("lkid") > -1){
$(this).val('new id');
alert($(this).val());
}
if($(this).attr('id').indexOf("lkold") > -1){
$(this).val('new text');
alert($(this).val());
}
})
})
})
})
Upvotes: 1
Views: 135
Reputation: 119
Try this: DEMO
$(document).ready(function () {
$('.selectClass').change(function() {
$(".owner input[id$='_lkid']").val('new id');
$(".owner input[id$='_lkold']").val('new text');
$(".owner span input[type=text]").val('new value');
})
})
Upvotes: 1
Reputation: 3330
You just need to use some selectors for that like,
You can get more such selectors from here -> http://www.w3schools.com/css/css_attribute_selectors.asp
Basically, jQuery selectors are CSS selectors...
Upvotes: 0
Reputation: 2502
You can use the following selectors.
select hidden input with id containing 'lkid':
$('[id*=lkid]')
select hidden input with id containing 'lkold':
$('[id*=lkold]')
select text input:
$('td.owner>span.lookupInput input[type=text]')
Upvotes: 2
Reputation: 133403
You can use .find()
with class selector along with child selector and Attribute Ends with selector
var textInput = $(this).find('.lookupInput input');
alert(textInput.val());
//Similarly you can use
var lkid = $(this).find("input[id$='_lkid']");
var lkold = $(this).find("input[id$='_lkold']");
Upvotes: 2
Reputation: 388316
If you want to assign the same text/value to all inputs then
$(document).ready(function () {
$('.selectClass').change(function () {
$('.owner input[id$="lkid"]').val('new id');
$('.owner input[id$="lkold"]').val('new text');
$('.owner input:text').val('new val');
})
})
Demo: Fiddle
Upvotes: 2
Reputation: 78525
You can use the attribute ends with selector:
var $lkid = $(this).find("input[id$='_lkid']"),
$lkold = $(this).find("input[id$='_lkold']"),
$text = $(this).find("input:text");
Upvotes: 2