1

I'm looking for a way to have a javascript/jquery function that references dynamically created html.

Essentially I have a form that is created dynamically through $('#list').append('an html form'). And then a function that looks something like:

function add_repo(id){
    $.ajax({
        url:'/add_project/',
        datatype:'json',
        data:{'name': $('#'+id).value()},
        success:function(data){
            alert(data);
        },
    })
}

where id is a dynamically created identifier for each form created by the jquery append function.

But add_repo() causes an error in the javascript as the jquery id identifiers don't exist initially. How should situations like this be handled?

2
  • How are you calling it onload? Commented Sep 3, 2011 at 3:12
  • both functions are currently in the head. moving the add_repo to the body solved the problem... that was easy enough. Commented Sep 3, 2011 at 3:18

2 Answers 2

1

change

data:{'name': $('#'+id).value()},

to

   data:{'name': $('#'+id).val()},
Sign up to request clarification or add additional context in comments.

Comments

0

Make a function that is called at runtime:

function add_repo(id){
    $.ajax({
        url:'/add_project/',
        datatype:'json',
        data:{'name': function() {
           return($('#'+id).value());
         }},
        success:function(data){
            alert(data);
        },
    })
}

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.