Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi to all.

i am making following function:

<pre lang="cs">function generatesciprtofwidget()
{
 //facebook like code
var facebooklike="you like facebookbutton";
document.getElementById('getlikecode').innerHTML = facebooklike; 
//getlikecode is a div which is located in body tag
alert(facebooklike);

return false;

 //facebook like code end
}



in body tag i am giving value to div
<pre lang="xml"><div id="getlikecode">
    <script>
     alert(document.getElementById('getlikecode').value)
    </script>
    </div>





On click button i call the generatesciprtofwidget() function here is code

<input id="Submit1" onclick="generatesciprtofwidget();"  type="submit" value="submit" />


now the problem is alert show undefine why please help!
Posted

Probably because the HtmlElement you're returning with document.getElementById('getlikecode') doesn't have a property called value. You're setting it by writing to the innerHTML property, so read it back out like that too:

XML
<div id="getlikecode">
    <script>
     alert(document.getElementById('getlikecode').innerHTML);
    </script>
    </div>
 
Share this answer
 
Comments
balongi 6-May-11 5:28am    
ok thanks
Sergey Alexandrovich Kryukov 6-May-11 10:15am    
You're right about "value" (should not be used at all), but I don't see any sense in this alert.
Please see my answer.
--SA
I think the problem is putting script in the div in question.

Think of it: you're putting the script as inner HTML of the element which you want to modify with other script. I don't see a sense.
Put the script in the HTML head (I would recommend, always). The element div always have innerHTML (you can make if empty in your HTML code).

How, Jim is right about value, you should not use it for div. You modification code is correct.
XML
document.getElementById('getlikecode').innerHTML = facebooklike;

I recently tested such code, it works.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900