FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

what's wrong with this code?

 


morfeusman
I'm having trouble with this code, i can't find the error.
Does anyone else see one?

Code:
<html>
<head>
<script type="text/javascript">

//changeing the text
function changeit()
{
var myvar=document.getElementById('slct2').value
if (myvar>1)
{
document.getElementById('td2').value=document.getElementById('mytext1').value;
alert('the text was changed');
}
else
{
document.getElementById('td1').value=document.getElementById('mytext1').value;
alert('the text was changed');
}
}



function alertInnerHTML() //displaying function
{
var slct=document.getElementById("text1").value; //set the value to check
if (slct>1)
{
alert("You selected 2");
alert(document.getElementById("td2").innerHTML); //alert row 1 column 1
}
else
{
alert(document.getElementById("td1").innerHTML);//alert row 1 column 2
}
}
</script>
</head>
<body>

<table border="1">
<tr>
<th>Firstvalue</th>
<th>Secondvalue</th>
</tr>
<tr>
<td id="td1">Myfirstvalue</td>
<td id="td1">mysecondvalue</td>
</tr>
</table>
<br />
<p> select the number of the value you want to display.</p>
<br />
<input type="button" onclick=alertInnerHTML() value="Alert innerHTML" />
<select id="slct1"><option>1</option><option>2</option></select>
<br />
<p> Or, you can select the number of the text and change it</p>
<br />
<input type="button" value="change value"  onclick="changeit()"/>
<select id="slct2"><option>1</option><option>2</option></select>
text to change it to:<input type="text" id="mytext1" />
<br />
<p id="message"><p>
</body>
</html>

please help it would be very much aprecciated.
fuhgeddaboutit
in the function changeit() you try to set a value to "td2"
document.getElementById('td2').value=document.getElementById('mytext1').value; <<<
document.getElementById('td1').value=document.getElementById('mytext1').value;

but your html shows
Code:
<td id="td1">Myfirstvalue</td>
<td id="td1">mysecondvalue</td>


Code:
var slct=document.getElementById("text1").value; //set the value to check

this should be mytext1, and the options of (my)text1 don't have values, so you could either give it a value, or ask for the innerHTML insteadf of the value.


this should do it
Code:

<html>
<head>
<script type="text/javascript">

//changeing the text
function changeit() {
   //~ var myvar=document.getElementById('slct2').value;// document.getElementById('slct2') doesn't have a Value... use innerHTML or assign a value
   var myvar=document.getElementById('slct2').value;// document.getElementById('slct2') doesn't have a Value... use innerHTML or assign a value
   document.getElementById('td' + myvar).innerHTML = document.getElementById('mytext1').value;
   alert('the text was changed');
}

//displaying function
function alertInnerHTML(){
   var slct=document.getElementById("slct1").value; //set the value to check
   alert(document.getElementById("td" + slct).innerHTML); //alert row 1 column 1
   
}
</script>
</head>
<body>

<table border="1">
<tr>
<th>Firstvalue</th>
<th>Secondvalue</th>
</tr>
<tr>
<td id="td1">Myfirstvalue</td>
<td id="td2">mysecondvalue</td>
</tr>
</table>
<br />
<p> select the number of the value you want to display.</p>
<br />
<input type="button" onclick="alertInnerHTML();" value="Alert innerHTML" />
<select id="slct1"><option value="1">1</option><option value="2">2</option></select>
<br />
<p> Or, you can select the number of the text and change it</p>
<br />
<input type="button" value="change value"  onclick="changeit()"/>
<select id="slct2"><option value="1">1</option><option value="2">2</option></select>
text to change it to:<input type="text" id="mytext1" />
<br />
<p id="message"><p>
</body>
</html>
Marcuzzo
get a good text editor like SciTE (google it)
in IE you can use the built in debugger.
in FF there surely are javascript debuggers.
check the line where the error is seen and you will surely find it.
what I do to debug is use a lot of alert() boxes to tell me what value's are beeing held in variables and function returns
morfeusman
Thanks for helping me, its really great.
i have FF, but i didn't know there was a debugger. can you tell me how to get to the debugger?
and ill get SciTE.
Thanks again!
Marcuzzo
morfeusman wrote:
Thanks for helping me, its really great.
i have FF, but i didn't know there was a debugger. can you tell me how to get to the debugger?
and ill get SciTE.
Thanks again!


FF plugin: https://addons.mozilla.org/en-US/firefox/addon/216

Scite: http://www.scintilla.org/SciTEDownload.html
halilibo
to find javascript or css errors of a page in FF, just type javascript: into the address bar.
this should show you the errors in your code.

and you can use alerts as Marcuzzo said. that can show you where the problem begins.
binsmyth
that is quite a good technique and a good practice for me thanks I will try it in my computer.
Related topics

I havent got my account handled nor tested this code...
PHP: Feedback form issues.. help =P
Upload Script (Read the 3rd post)
WEBSITE HELP NEEDED!
Court shuts down German wikipedia site

PHP issue
Iframes?
include() for phpbb = blank?
Can't get strings to output correctly
dabbling in CSS (or trying to anyways lol)

mysql wont delete [solved]
passing input from a form to a script
HOW TO LISTEN MP3 AUDIO WITH HTML
What does this mean?
Don't know what's wrong

MySQL Problem
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.