2

I'm appending values into a div through jQuery, but I've realized what gets appended isn't affected by my javascript functions.

$(".div").append("<input type='text' class='textForm' placement='Value' />");

What I have setup in my javascript code is that it takes the attribute placement of any class "textForm" and makes it a value. But I've realized that once a value is appended, it isn't effected by my javascript. Any ideas on how to fix this issue?

3
  • Can you please show us the other code you are talking about as well? Commented Jul 7, 2012 at 14:33
  • It is interesting, that class name is 'div'. Commented Jul 7, 2012 at 14:35
  • I think what @eng is trying to say is that it might be preferable to choose a more descriptive name for a class. Right now all we can deduct from the name is that it is a div element. Why not give it a more descriptive name like textFormHolder. It will make your code more readable and easier to extend/maintain. Commented Jul 7, 2012 at 14:36

3 Answers 3

4

If you are currently using

$(".textForm").click(){}

then now use

$(document).on("click",".textForm",function(){//Dtuff here})

This will attach the .on("click") to the document object and as such it will be enabled on all elements that exist and all elements that are created matching the .textForm selector.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually, I'm using a function in my included javascript file, then calling it to a button like so: <input type="button" onclick="addNew()" />
You can't do that you have to write that function in main page , dude you are using jquery then dump this old way of binding event
Ah ok, well I wanted to keep organized like that. I had no idea that was the "old way" of doing it.
It works just fine in Chrome,FF and IE on the the desktop,however IT doesn't on an iphone (safari), are there any suggestion?
3

I guess you have some events bounded to some elements like which are not working after the append . something like this.

$(function(){
    $(".someClass").click(function(){
       //dome some thing
    });
});

If you want the same functionality to work on the newly injected( dynamically added via append /jquery ajax etc...) DOM elements, you should consider using jquery on. So your code should be changed like this

$(function(){
    $(document).on("click",".someClass",function(){
       //dome some thing
    });
});

on will work for current and future elements

1 Comment

Thanks, that makes more sense to me!
0

I'm not sure I understand the bit about why you're copying values from the placement attribute into the input value, but I can offer this suggestion to get your form fields to appear.

$("div").each(function() {
    $(this).append($("<input type='text' class='textForm' placement='Value' />"))
});

I'm assuming that you want to identify your div via the tag name, and not the class name. If this is the case, your jQuery selector will need to be "div", and not ".div". Also, you need to wrap your HTML in $() in order to generate a DOM element.

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.