/    Sign up×
Community /Pin to ProfileBookmark

Trying to set a variable in a value for debug purposes

Eh,I’m pretty new at Javascript and sometimes I stumble on simple stuff. I’ve been messing around with this but Im probably just not doing right.
I tried the (standard?) document.write() but didnt help me much (interrupted the code) so I was trying to change a value in an empty field I have just to test a little counter I had to do.

Here is the code:

[CODE]
var headerArray = new Array(“Author”, “Title”, “Year”, “Amount”);
var classNamesArray = new Array(“authorClass”, “titleClass”, “yearClass”, “amountClass”, “buttonClass”);
var counter = 0;

function update(number) {
counter = counter + number;
[COLOR=”RoyalBlue”]document.getElementById(berror).value = counter;[/COLOR]
}

function moreFields() {
howMany = parseInt(document.getElementById(‘howMany’).value);
update(howMany);
for (var ii = 0; ii < howMany; ii++) {
dynamicParent = document.getElementById(‘dynamicParent’);
index = dynamicParent.rows.length;
dynamicParent.insertRow(index);
for (var i = 0; i < 5; i++) {
dynamicParent.rows[index].insertCell(i);
if (i < 4) {
textField = document.createElement(‘input’);
textField.type = ‘text’;
textField.name = headerArray[i] + index;
dynamicParent.rows[index].cells[i].appendChild(textField);
textField.className = classNamesArray[i];
} else {
dynamicParent.rows[index].cells[i].className = ‘buttonClass’;
dynamicParent.rows[index].cells[i].innerHTML = ‘Remove Field’;
dynamicParent.rows[index].cells[i].onclick = function() {

if (this.parentNode.parentNode.rows.length > 2) {
this.parentNode.parentNode.removeChild(this.parentNode);
update(-1);
}
}
}
}
}

document.getElementById(‘howMany’).value = ‘1’;
}

</script>
</head>

<body onload=”moreFields()”>

<form action=”form.php” method=”post” name=”data” id=”1″>
<table width=”200″ align=”center”>
<tr>
<th scope=”col”><img src=”https://files.getdropbox.com/u/1531805/upbanner.png” alt=”text” /></th>
</tr>
<tr>
<td>
<table width=”974″>
<tr>
<td width=”135″ align=’right’><label>Order Date : </label></td>
<td><input name=”Order date:” type=”text” class=”txtbox”/></td>
</tr>
<tr>
<td align=’right’><label>Teacher Name : </label></td>
<td><input name=”Tname” type=”text” class=”txtbox” /></td>
</tr>
<tr>
<td align=’right’><label>Subject : </label></td>
<td><input name=”Subject” type=”text” class=”txtbox” /></td>
</tr>
</table>
<br>
</td>
</tr>
<tr>
<td>
<table width=”974″ border=”0px” cellspacing=”1″ id=”dynamicParent”>
<tr>
<td align=”center” class=”td authorClass”><label>Author</label></td>
<td align=”center” class=”td titleClass” ><label>Title</label></td>
<td class=”td amountClass” ><label>Year</label></td>
<td class=”td amountClass” ><label>Amount</label></td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr style=”border:none”>
<td ><input style=”border:none”class=”authorClass” id=”aerror” onfocus=”this.blur();”/>&nbsp;</td>
<td ><input style=”border:none”class=”titleClass” id=”terror” onfocus=”this.blur();”/>&nbsp;</td>
<td ><input style=”border:none”class=”yearClass” id=”yerror” onfocus=”this.blur();”/>&nbsp;</td>
<td ><input style=”border:none”class=”yearClass” id=”aerror” onfocus=”this.blur();”/>&nbsp;</td>
[COLOR=”RoyalBlue”]<td ><input style=”border:none”class=”yearClass” id=”berror” onfocus=”this.blur();”/>&nbsp;</td>[/COLOR]
</tr>
</table>
</td>
<tr>
<td>
<table>
<tr>
<td style=”width:815px” align=”right”><input type=’text’ id=’howMany’ value=’1′ size=’2′ />
<input type=”button” onclick=”moreFields()” value=”More Field(s)” />
<input type=”submit” value=”Send” /></td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</form>[/CODE]

Any hints would be appreciated,I have painted blue the important part.
Oh ye,the error I get is that ‘berror’ is not defined

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ntk418Jul 24.2009 — Hello again.

I believe the reason it is giving that error is because of the following:

I have drawn it red.

[CODE]
function update(number) {
counter = counter + number;
document.getElementById[COLOR="Red"](berror)[/COLOR].value = counter;
}
[/CODE]


Again this is a problem with missing quotations, it is currently trying to use a variable named berror which has not yet been defined. Add some quotes around it as we did with the array previously:

Also, I used the += operator, it basically says the exact same thing you have written, (it's very similar to ++ except it increments the specified number)

[CODE]
function update(number) {
[COLOR="Blue"]counter += number;[/COLOR] [COLOR="Lime"]// this says the same thing as counter = counter + number; I think it just looks neater[/COLOR]
document.getElementById([COLOR="Blue"]'berror'[/COLOR]).value = counter;
}
[/CODE]


Note that your code would have worked had you a variable named berror such as:

[CODE]
var berror = 'berror';
[/CODE]


somewhere in the code. But that is unnecessary since you can just put quotes around it in the getElementById() function, I was just pointing that out to give you a reference.

HTH
Copy linkTweet thisAlerts:
@AuchiauthorJul 25.2009 — Ah,thanks for both, I have to make a mental note on quotations :S

counter += number; looks better indeed.
×

Success!

Help @Auchi 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.18,
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,
)...