user1688181
user1688181

Reputation: 492

Find input field inside a div Tag using jQuery

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

Answers (7)

SK.
SK.

Reputation: 4358

document.getElementById('frame').value; will give you the value. Now you can change dynamically...

Upvotes: 0

Supraja Suresh
Supraja Suresh

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

Ashu
Ashu

Reputation: 36

Try this

var guest_html= $('#guests_feilds1');

var inputT = $(this).find('input[name="name"]');

console.log(inputT)

Upvotes: 0

Dhanu Gurung
Dhanu Gurung

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()

DEMO

Upvotes: 0

Ankur Jain
Ankur Jain

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

xdazz
xdazz

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

Aashray
Aashray

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

Related Questions