简体   繁体   中英

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?

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

Change

var guest_html=$('#guests_feilds1').html();

To

var guest_html=$('#guests_feilds1');

You need to get the object reference, not the inner content.

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);          
});

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

Try this

var guest_html= $('#guests_feilds1');

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

console.log(inputT)

如果只有一个ID为'fname'的元素,则可以直接使用document.getElementById('fname'),而不是在一个对象上调用它

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM