tags:

views:

34

answers:

3

I have used jquery to load some html from another page and added the content to a div called 'console'. This happens when I press a link: $ ('# Console'). Load (this.href + '# container');

I try then to retrieve the form which is part of the added html. I use $('#formname') but I can not get the form element.

Why? What should I do?

A: 

Check your html you are putting in does the form also have an id attribute? As # is used in jQuery as the id selector.

$("[name=formname]") // if you need to use the name

Selectors: http://api.jquery.com/category/selectors/

Thomas James
Yes, it is on purpose that I use #.
A: 

The .load() method is lower case, like this:

$('#Console').load(this.href + ' #container');

Also make sure you're looking for it after it loads, like this:

$('#Console').load(this.href + ' #container', function() {
  var form = $('#formid');
});
Nick Craver
A: 

Make sure you are trying to retrieve the form on complete of the ajax load. Ajax is happening asynchronously remember.

should look like this:

$('#Console').load(this.href + ' #container', function(){
    $('#formname'); // get form here
});

also check you syntax in the load url string. Need to have a space before the '#container'

Josiah Ruddell
I think the problem is that the new html is not loaded in the DOM. What should I do?
if you moved the form selector into the callback function then there must be a different problem. Make sure your selectors are right. There needs to be an element id="Console" somewhere on the page. check the url, and make sure the ajax load page has an id="container" on it.
Josiah Ruddell
$(document).ready(function () { $("a").click(function () { $('#console').load(this.href + ' #container', function () { alert($('#side2form').id); }); return false; }); });
the $('#side2form') cannot be found. alert($('#side2form').id) returns undefined. The returned html has the element 'side2form'.
should be $('#side2form').attr('id'). And also look at the this object inside the success function. you could do $(this).find('#side2form'). this is the loaded content from ajax inside the success function. See jquery load api on jquery site for more information.
Josiah Ruddell