0

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?

1
  • what is fname? id or name of input field? Commented Jan 17, 2014 at 7:13

7 Answers 7

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Change

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

To

var guest_html=$('#guests_feilds1');

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

Comments

0

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

Comments

0

Try this

var guest_html= $('#guests_feilds1');

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

console.log(inputT)

Comments

0

If there is only one element with id 'fname', then you can directly use document.getElementById('fname'), rather calling it on an object

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.