Accessing HTML elements generated dynamically

Ok I am using JQuery to dynamically generate some input elements of type button. In a nutshell what I am doing is I have a textbox where i input any text. I have a DB where I want to save each of the words entered in the textbox. For each word I display a table with each row containing a word found (seperated by a space) and right next to the word a button 'Add'.

Lets say I enter "hellothere" then I type a space like so -> "hello there". On the fly the two rows are generated one "hello"
and another "there" .

For somereason the JQuery doesnt perform the action i want it to perform when ever a button class addword is clicked

$('.addword').click(function() {alert("hi");});

I am thinking that the reason the element cannot be accessed is because it was dynamically generated afterward and so javascript cant find it? I know that I am asking about JQuery but that is just a javascript library - have any of you had problems grabbing HTML elements on a page that were DYNAMICALLY generated with javascript with onClick, onChange, etc?

Please any help will be GREATLY APPRECIATED!!

Comments

  • : Ok I am using JQuery to dynamically generate some input elements of
    : type button. In a nutshell what I am doing is I have a textbox where
    : i input any text. I have a DB where I want to save each of the words
    : entered in the textbox. For each word I display a table with each
    : row containing a word found (seperated by a space) and right next to
    : the word a button 'Add'.
    :
    : Lets say I enter "hellothere" then I type a space like so -> "hello
    : there". On the fly the two rows are generated one "hello"
    : and another "there" .
    :
    : For somereason the JQuery doesnt perform the action i want it to
    : perform when ever a button class addword is clicked
    :
    : $('.addword').click(function() {alert("hi");});
    :
    : I am thinking that the reason the element cannot be accessed is
    : because it was dynamically generated afterward and so javascript
    : cant find it? I know that I am asking about JQuery but that is just
    : a javascript library - have any of you had problems grabbing HTML
    : elements on a page that were DYNAMICALLY generated with javascript
    : with onClick, onChange, etc?
    :
    : Please any help will be GREATLY APPRECIATED!!

    Just a guess... try $('.addword').click() = function() {alert("hi");};

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion