/    Sign up×
Community /Pin to ProfileBookmark

Dom output causing problems to my script

Hi all, I’ve been developing a Javascript app, and I had it working properly but using old skool methods. Like hardcoding HTML attributes in my JS. So I’ve attempted to re-write it but it’s breaking I dont suppose someone could tell me where I’m going wrong. It’s got something to do with the sorting I’m sure of it.

[b]Original Working Source[/b]

[code]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<body>
<h2>Prices Array Printing/Sorting</h2>
<script type=”text/javascript”>
function dynamicPricing (localeName,localePrice, localeGroup, localeHref) {
this.localePrice = localePrice;
this.localeName = localeName;
this.localeGroup = localeGroup;
this.localeHref = localeHref;
}

dynamicPricing.prototype.getLocalePrice = function() {
return this.localePrice;
}

dynamicPricing.prototype.setLocalePrice = function(localePrice) {
this.localePrice = localePrice;
}

dynamicPricing.prototype.getLocaleGroup = function() {
return this.localeGroup;
}

dynamicPricing.prototype.setLocaleGroup = function(localeGroup) {
this.film = localeGroup;
}

dynamicPricing.prototype.getLocaleName = function() {
return this.localeName;
}

dynamicPricing.prototype.setLocaleName = function(localeName) {
this.localeName = localeName;
}

dynamicPricing.prototype.getLocaleHref = function() {
return this.localeHref;
}

dynamicPricing.prototype.setLocaleHref = function(localeHref) {
this.localeHref = localeHref;
}

function localeStore()
{
this.localeArr = new Array();
}

localeStore.prototype.addLocale = function(localeName, localePrice, localeGroup, localeHref)
{
this.localeArr[this.localeArr.length] = new dynamicPricing(localeName, localePrice, localeGroup, localeHref);
}

localeStore.prototype.getBookingsTable = function()
{
this.localeArr.sort(this.sortLocale)
var bookingsTableHTML = “<table border=1>”;
var currentLocale;
for (var i = 0; i < this.localeArr.length; i++)
{
if (this.localeArr[i].getLocaleName() != currentLocale)
{
if (currentLocale != “”) {
bookingsTableHTML += “</tr>”
bookingsTableHTML += “<tr>”
}
}
currentLocale = this.localeArr[i].getLocaleName();

bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleName();
bookingsTableHTML += “</td>”;

bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocalePrice();
bookingsTableHTML += “</td>”;

bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleGroup();
bookingsTableHTML += “</td>”;

bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleHref();
bookingsTableHTML += “</td>”;

}
bookingsTableHTML += “</table>”;
return bookingsTableHTML;
}

localeStore.prototype.sortLocale = function(a,b){
if (a.getLocaleGroup() < b.getLocaleGroup() /*&& a.getLocaleName() < b.getLocaleName()*/)
return -1
if (a.getLocaleGroup() > b.getLocaleGroup() /*&& a.getLocaleName() > b.getLocaleName()*/)
return 1
return 0
}

var pricingTable = new localeStore();
pricingTable.addLocale(“malaga”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“orlando”, “£199”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“london”, “£29”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“paris”, “£49”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“sydney”, “£299”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“las vegas”, “£599”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“tokyo”, “£199”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“rome”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“boston”, “£399”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“buenos aires”, “£999”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“manchester”, “£10”, “Europe”, “http://www.google.co.uk/”)

document.write(pricingTable.getBookingsTable());
</script>
</body>[/code]

[b]New Non-Working Code[/b]

[code]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<body>
<h2>Prices Array Printing/Sorting</h2>
<style type=”text/css”>
#dpTable {width: 150px; border-collapse: collapse;}
#destStyle {color: #666666; font-weight: bold; font-size: 9pt; font-family: Arial, Helvetica, sans-serif;}
#priceStyle {color: #ff0000; font-weight: bold; font-size: 9pt; font-family: Arial, Helvetica, sans-serif;}
</style>
<script type=”text/javascript”>
function dynamicPricing (localeName,localePrice, localeGroup, localeHref) {
this.localePrice = localePrice;
this.localeName = localeName;
this.localeGroup = localeGroup;
this.localeHref = localeHref;
}

dynamicPricing.prototype.getLocalePrice = function() {
return this.localePrice;
}

dynamicPricing.prototype.setLocalePrice = function(localePrice) {
this.localePrice = localePrice;
}

dynamicPricing.prototype.getLocaleGroup = function() {
return this.localeGroup;
}

dynamicPricing.prototype.setLocaleGroup = function(localeGroup) {
this.film = localeGroup;
}

dynamicPricing.prototype.getLocaleName = function() {
return this.localeName;
}

dynamicPricing.prototype.setLocaleName = function(localeName) {
this.localeName = localeName;
}

dynamicPricing.prototype.getLocaleHref = function() {
return this.localeHref;
}

dynamicPricing.prototype.setLocaleHref = function(localeHref) {
this.localeHref = localeHref;
}

function localeStore()
{
this.localeArr = new Array();
}

localeStore.prototype.addLocale = function(localeName, localePrice, localeGroup, localeHref)
{
this.localeArr[this.localeArr.length] = new dynamicPricing(localeName, localePrice, localeGroup, localeHref);
}

localeStore.prototype.getBookingsTable = function()
{
var getBody = document.getElementsByTagName(‘body’)[0];

var dpTbl = document.createElement(‘table’);
dpTbl.id = ‘dpTable’;
var dpTblBdy = document.createElement(‘tbody’);

this.localeArr.sort(this.sortLocale)

var currentLocale;
for (var i = 0; i < this.localeArr.length; i++)
{
if (this.localeArr[i].getLocaleName() != currentLocale)
{
if (currentLocale != “”) {
dpTblBdy.appendChild(dpRow);
var dpRow = document.createElement(‘tr’);
}
}
currentLocale = this.localeArr[i].getLocaleName();

var dpRow = document.createElement(‘tr’);

var dpCol1 = document.createElement(‘td’);
var dpCol1L = document.createElement(‘a’);
dpCol1.appendChild(dpCol1L);
dpCol1L.href = this.localeArr[i].getLocaleHref();
dpCol1L.innerHTML = this.localeArr[i].getLocaleName();
dpCol1L.id = ‘destStyle’;
dpRow.appendChild(dpCol1);

var dpCol2 = document.createElement(‘td’);
var dpCol2L = document.createElement(‘a’);
dpCol2.appendChild(dpCol2L);
dpCol2L.href = this.localeArr[i].getLocaleHref();
dpCol2L.innerHTML = this.localeArr[i].getLocalePrice();
dpCol2L.id = ‘priceStyle’;
dpRow.appendChild(dpCol2);

dpTblBdy.appendChild(dpRow);

}
dpTbl.appendChild(dpTblBdy);
getBody.appendChild(dpTbl);
}

localeStore.prototype.sortLocale = function(a,b){
if (a.getLocaleGroup() < b.getLocaleGroup() /*&& a.getLocaleName() < b.getLocaleName()*/)
return -1
if (a.getLocaleGroup() > b.getLocaleGroup() /*&& a.getLocaleName() > b.getLocaleName()*/)
return 1
return 0
}

var pricingTable = new localeStore();
pricingTable.addLocale(“malaga”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“orlando”, “£199”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“london”, “£29”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“paris”, “£49”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“sydney”, “£299”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“las vegas”, “£599”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“tokyo”, “£199”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“rome”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“boston”, “£399”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“buenos aires”, “£999”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“manchester”, “£10”, “Europe”, “http://www.google.co.uk/”)

window.onload = pricingTable.getBookingsTable();
</script>
</body>[/code]

Many Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423Sep 27.2006 — replace this part
[CODE]if (currentLocale != "") {
dpTblBdy.appendChild(dpRow);
var dpRow = document.createElement('tr');[/CODE]

with
[CODE]if (currentLocale != "") {
if(typeof currentLocale!= "undefined")
dpTblBdy.appendChild(dpRow);
var dpRow = document.createElement('tr');[/CODE]

also..

window.onload = function(){pricingTable.getBookingsTable();}
Copy linkTweet thisAlerts:
@gingerjauthorSep 27.2006 — also..

window.onload = function(){pricingTable.getBookingsTable();}[/QUOTE]


can I just ask what's the difference about this code and mine?

oh and thanks ?
Copy linkTweet thisAlerts:
@sridhar_423Sep 27.2006 — not sure.. but I can give my own explanation for this as per my understanding..

when onload is used..

if you're assigning some function .. like the way in which u assingned, it will try to execute the function represented by tat name but the variables which are used in that function and initialised outside the function will not have any identity. i.e. they result in "undefined" value. Sort of like having "super" in a constructor and "static variables" in that class.

if you're not using any variables that are defined outside the function, then ur assignment will work fine.

Now, why "function()" works...

before it creates some new function, the varibles defined outsode gets intialized. Equivalent to initialising Static Variable before an overridden constructor is called.. like
[CODE]class Test{
static String a="k";
Test(){
this(b);
}
Test(String b){
k=b;
}
}[/CODE]

got the idea.. sorry to club the answer with Java concepts..

but cudn't think of a way to explain more clearly than this..
×

Success!

Help @gingerj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...