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?
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?
<!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>
Bookmarks