Reputation: 492
In my jQuery function I read the div tag into variable. There are many text fields inside that div tag. I want to get specific field by its name.
Tag:
var guest_html=$('#guests_feilds1').html();
Get field:
guest_html.getElementById('fname');
But this will return error "has no method 'getElementById' ".
I just want to get the "fname" field and change its name dynamically.How can i do that?
Upvotes: 0
Views: 4636
Reputation: 4358
document.getElementById('frame').value; will give you the value. Now you can change dynamically...
Upvotes: 0
Reputation: 49
If there is only one element with id 'fname', then you can directly use document.getElementById('fname'), rather calling it on an object
Upvotes: 0
Reputation: 36
Try this
var guest_html= $('#guests_feilds1');
var inputT = $(this).find('input[name="name"]');
console.log(inputT)
Upvotes: 0
Reputation: 8858
You can try as:
var div = $('#guests_feilds1');
# finding using ID:
div.find('#input1').val()
# finding using name:
div.find("input[name='your_input_name']").val()
Upvotes: 0
Reputation: 1404
I think you should try like this.
$(document).ready(function(){
var parentHtmlTag = $('#guests_feilds1');
// Getting fname value
var fname = parentHtmlTag.find('#fname').val();
alert('First Name : '+fname);
// Setting value of fname dynamically
var newFname = parentHtmlTag.find('#fname').val('Test');
alert('New First Name : '+newFname);
});
Upvotes: 1
Reputation: 160993
guest_html
is a string so doesn't have method getElementById
.
What you need is:
// since you use getElementById('fname'), I assume fname is an id
$('#fname').attr('name', 'foo'); // change the name of the element with id fname
Upvotes: 2
Reputation: 2763
Change
var guest_html=$('#guests_feilds1').html();
To
var guest_html=$('#guests_feilds1');
You need to get the object reference, not the inner content.
Upvotes: 0