I am using this code to generate hidden HTML code in Facebook :
echo "<div id=\"y257y\" style=\"display:none;\">".$fdesc."</div>";
And, I am trying to get this element back in JS using the following code
newVal=document.getElementById('y257y').getValue();
But, I am getting an error: Undefined
Can anyone kindly help me out ?
Thanks.
- ahsan
I am using this code to generate hidden HTML code in Facebook :
echo "<div id=\"y257y\" style=\"display:none;\">".$fdesc."</div>";
And, I am trying to get this element back in JS using the following code
newVal=document.getElementById('y257y').getValue();
But, I am getting an error: Undefined
Can anyone kindly help me out ?
Thanks.
- ahsan
Share Improve this question edited Mar 13, 2011 at 2:49 Ahsan asked Mar 13, 2011 at 2:18 AhsanAhsan 2,98212 gold badges54 silver badges97 bronze badges 6-
Just a quick remendation:
echo "<div id='y257y' style='display:none;'>".$fdesc."</div>";
– JCOC611 Commented Mar 13, 2011 at 2:24 -
1
Even better:
<div id="y257y" style="display:none;"><?php echo $fdesc; ?></div>
| What makes you think the exists the functiongetValue()
? – Felix Kling Commented Mar 13, 2011 at 2:29 - @Felix Kling: I am working wth FBJS and theres a function getValue() as mentioend here : developers.facebook./docs/fbjs – Ahsan Commented Mar 13, 2011 at 2:48
-
@Ahsan: I see. But this table shows a translation of JavaScript attributes to FBJS methods. The attribute
value
translates togetValue()
and only form elements have this attribute. It was never possible to get the content of an arbitrary element withvalue
. From what I read on this page, it seems you just can't access the the HTML. I hope you also read the note at the top. It seems the don't remend to use FBML anymore. – Felix Kling Commented Mar 13, 2011 at 2:53 - Hidden HTML in Facebook, eh? Sounds ominous. – Lightness Races in Orbit Commented Mar 13, 2011 at 3:36
2 Answers
Reset to default 4Instead of:
newVal = document.getElementById('y257y').getValue();
try using:
newVal = document.getElementById('y257y').innerHTML;
Are you using any JavaScript library, like jQuery or Prototype? If you're using jQuery:
newVal = $('#y257y').html();
Other suggestions:
Use hidden form element:
echo "<input type=hidden id=y257y value=\"$fdesc\">";
and in JavaScript:
newVal = document.getElementById('y257y').value;
Or just output a <script>
tag:
echo "<script>newVal = \"$fdesc\";</script>";
and there's no need to find the value in the DOM – it's already in JavaScript.
Try using a textarea? Since at least that may have a getValue method if you are really lucky:
$fdesc = str_replace('<', '<', $fdesc);
$fdesc = str_replace('>', '>', $fdesc);
echo "<textarea id=\"y257y\" style=\"display:none;\">".$fdesc."</textarea>";
newVal=document.getElementById('y257y').getValue();
newVal = newVal.replace(">", ">")
newVal = newVal.replace("<", "<")
But something tells me that is not going to work for you, facebook may not like it. Seems easier just to do this:
$fdesc = <<<EOD
<div>Here is your html</div>
<p>And some more </p>
EOD;
$fdesc = str_replace("'", "\'", $fdesc); //Escape single quotes
$fdesc = str_replace("\n", "";', $fdesc); //Get rid of line breaks
echo "var fdesc = '$fdesk';";