www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to get the content of ane element that called a function?

  1. #1
    Join Date
    Jan 2013
    Posts
    84

    How to get the content of ane element that called a function?

    Hi
    I've a code that creates a series of div element with some texts such innerhtml. I've added an onClick event to each of them, but actually i need to retrieve the text contained into the elmenet that called the funcion.

    This is the code that creates dinamically the elements

    PHP Code:
      var elm=document.createElement('div');
      
    elm.innerHTML=xmlhttp.responseText//response from an ASP script called with AJAX
      
    elm.setAttribute('onclick''prova(str)');
      
    document.getElementById("risultatiRicerca").appendChild(elm)


    function 
    prova(str)
    {
        
    alert("Text into element: " str);


    How i can do it? Is it possibile or i've to create a "value"attribute for each element?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Question

    Quote Originally Posted by American horizo View Post
    Hi
    I've a code that creates a series of div element with some texts such innerhtml. I've added an onClick event to each of them, but actually i need to retrieve the text contained into the elmenet that called the funcion.

    This is the code that creates dinamically the elements

    PHP Code:
      var elm=document.createElement('div');
      
    elm.innerHTML=xmlhttp.responseText//response from an ASP script called with AJAX
      
    elm.setAttribute('onclick''prova(str)');
      
    document.getElementById("risultatiRicerca").appendChild(elm)


    function 
    prova(str)
    {
        
    alert("Text into element: " str);


    How i can do it? Is it possibile or i've to create a "value"attribute for each element?
    Can you provide a bit more of your code? The little snippet of code you posted does not do much by itself.

    What do you mean by "need to retrieve the text contained into the elmenet that called the funcion."?

    Are you asking to store the innerHTML contents of a <div> section into another element?

  3. #3
    Join Date
    Mar 2009
    Location
    Wherever i lay my hat
    Posts
    180
    Try:

    elm.setAttribute( 'onclick', 'prova(this.innerHTML)');

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Lightbulb

    Just a guess as to what you are trying to do ...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
    <div id="risultatiRicerca"></div>
    
    <script type="text/javascript">
    // simulation of 'xmlhttp.responseText;' //response from an ASP script called with AJAX
    var strMsg = ['Now','is','the','time','for','all','good','men','to','come'];
    
    for (var i=0; i<strMsg.length; i++) {
      var elm=document.createElement('div');
      elm.innerHTML = strMsg[i];  // xmlhttp.responseText; //response from an ASP script called with AJAX
      elm.setAttribute('onclick', 'prova(this.innerHTML)');
      document.getElementById("risultatiRicerca").appendChild(elm)
    }
    
    function prova(str) { alert("Text into element: " + str); }  
    </script>
    
    </body>
    </html>

  5. #5
    Join Date
    Jan 2013
    Posts
    84
    Yes this is what i lookning for

    For exemple, throught JS i create a div element like this: <div>Luca</div>

    Clicking on "Luca" i've to calla a function and know the name that called it... So i've to use innerHTML property
    thanks

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380
    Quote Originally Posted by American horizo View Post
    Yes this is what i lookning for

    For exemple, throught JS i create a div element like this: <div>Luca</div>

    Clicking on "Luca" i've to calla a function and know the name that called it... So i've to use innerHTML property
    thanks
    You're most welcome.
    Happy to help when I finally figure out what you are asking about.
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles