Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with java script function

Status
Not open for further replies.

meenu24

Programmer
May 5, 2006
37
US
<html>
<head>
<script type="text/javascript">
function gethelp(txt1)
{
try
{
var al
if(document.getElementById('raw').value == txt1)
{
document.write(txt1)
}
}
catch(err)
{
al = err.description
alert(al)
}
}
</script>
</head>
<body >
<table>
<tr ><td onclick="gethelp('hello')" id="one" >hi</td></tr>
<tr><td id="raw">hello</td></tr></table>
</body >
</html>
The above underlined java script code does not work. Am I doing anything wrong. Thanks in advance
 
You can't use document.write dynamically like that - only on page load. If you use document.write ANY time after the page has completed loading, then it will completely overwrite all the contents of your page.

Look into using innerHTML or DOM methods. (innerHTML is easier, and probably faster)

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Hi Kaht,

Thanks for your suggestion. But my question is
document.getElementById('raw').value == txt1, this statement does not work. THis does not work. For example I am posting the code below which gives no change when row with id raw is clicked.

<html>
<body>
<script>
function show(getter)
{
try
{
var al
document.getElementById('raw').value = getter
}
catch(err)
{
al = err.description
alert(al)
}
}
</script>
<table>
<tr>
<td id="raw" onclick="show('verygood')">correct</td>
</tr>
</table>
</body>
</html>
 
TD's do not have a value property so your if statment would not evaluate correctly.
Try accessing it's innerHTML property instead.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Thankyou very much. But as I am new to html and java script can you please tell me what is the innerHTML property means. Is there any way where i can change the value of a row field through java script

THanks


 
The TD tag itself has some properties but what you want is not part of the TD tag, it is what is inside the TD tag.
Think of the TD tag as a container. Inside that container you have text or HTML. To get at the HTML in there you would do almost exactly what you did but substitue innerHTML in place of value.
Code:
<html>
<head>
<script type="text/javascript">
function gethelp(txt1)
{
  try
  {
    var al
    if (document.getElementById('raw').innerHTML == txt1)
    {
      alert(txt1);
    }
  }
  catch(err)
  {
    al = err.description
    alert(al)
  }
}
</script>
</head>
<body >
<table>
<tr ><td onclick="gethelp('hello')" id="one" >hi</td></tr>
<tr><td id="raw">hello</td></tr></table>
</body >
</html>
[code]

If you wanted to set the content inside that TD tag you would use:
[code]
  document.getElementById('raw').innerHTML == txt1;

It's hard to think outside the box when I'm trapped in a cubicle.
 
Hi

I have one more question. Can onmouseover on a row perform two action, like changing the style of the row by loading the style class and calling a java script.
Thanks for your responses
 
Yes. When you use an event like onmouseover or onclick, etc you can put more than one command in there like this:

<tr id="somerow" onmouseover="this.style.color='red'; this.style.cursor='hand'">
You just have two javascript commands. Note that the cursor property is IE only.

Or you can call two functions:
<tr id="somerow" onmouseover="myfunc(); anotherfunc();">

Or just call one function but include everything you want to do in that function so you do not have to make your TR code overly long and complex.

Note though that when you apply the onmouseover event to the table row it will fire over any object in that row.
If you apply it to the TD and you have for instance an input field in that cell the onmouseover fires over that input field.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top