Get div value by id - Javascript
I'm trying to get the value of my div into Javascript. It's a Drupal website and I want to convert currencies and displaying them in a javascript popup.
So far I have this:
<?php
print "<script type='text/javascript'>";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";";
print "var $displaycur = getElementById('record-price');";
print "$displaycur;";
print "</script>";
?>
And all I get in my browers is:
<script type="text/javascript">
exchangerate = 0.8406;var = getElementById('record-price');;
</script>
Although I know the reason why it's displayed like this, I still don't know how to get the ID of the div in a proper variable. Any ideas? Should I use innerHTML?
UPDATE: A part of the source files: page.tpl.php
<?php
// $Id: page.tpl.php,v 1.18.2.1 2009/04/30 00:13:31 goba Exp $
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language ?>" lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>">
<head>
<?php print $head ?>
<title><?php print $head_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
<?php
// lets get the exchange rate from HKD to RMB, with the help of yahoo finance api and the currency api module
// we pass the value to javascript, the tooltip will handle the rest
print "<script type='text/javascript'>";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";";
print "</script>";
?>
<script type="text/javascript">
function showCurrency() {
alert('$rate');
}
</script>
<!--[if lte IE 7]>
<?php print phptemplate_get_ie_styles(); ?>
<![endif]-->
<!--[if IE 6]>
<style type="text/css">
.fg-menu-ipod .fg-menu li { width: 95%; }
.fg-menu-ipod .ui-widget-content { border:0; }
.views-field-field-performer-value { margin-left:-150px;}
.views-field-field-instruments-value { margin-left:-150px;}
.coda-nav{display:block; position:absolute; width:400px;height:20px;top:260px;right:100px;z-index:125421;} </style>
<![endif]-->
</head>
<body<?php print phptemplate_body_class($left, $right); ?>>
And the node-record.tpl.php:
print '<div id="part1-right-line3">';
print开发者_高级运维 '<div id="record-price">';
print uc_currency_format($node->sell_price);
print '</div>';
print '</div>';
print '<div id="part1-right-line4">';
print '<div id="add-to-cart">';
print drupal_get_form('uc_product_add_to_cart_form_'. $node->nid, $node);
print '</div>';
print '</div>';
print '</div>';
And here is the output:
Please tell me if you need more, cause there is :) Thanks in advance<?php
print "<script type='text/javascript'>\n";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";\n";
print "var displaycur = document.getElementById('record-price');\n";
print "alert(displaycur.innerHTML);\n";
print "</script>\n";
?>
I guess 'record-price' is ID of your DIV... so displaycur.innerHTML
will have everything between <div>
and </div>
.
EDIT:
function showCurrency() {
alert(document.getElementById('record-price').innerHTML);
}
Have you defined $displaycur
in your PHP code? Also, it should be document.getElementById
.
Update: All you need from PHP is the exchange rate. You can do everything else in JavaScript:
<script type="text/javascript">
var exchangerate = <?php echo currency_api_convert("HKD", "CNY"); ?>;
var div = document.getElementById('record-price');
var price = parseInt(div.innerHTML);
// convert and display
alert(price * exchangerate);
</script>
精彩评论