PDA

View Full Version : innerhtml help


sandeep89
06-21-2005, 04:16 PM
i hope this is in the right place, when i've bin looking for help on this the term 'DOM' has been shown a lot, so i think its here, if not, please let me know...

now, this is probably going to be really easy for you guys, i don't think that its much of a mission for those who know how to use javascript and that, by k..

i have the following:

<body>
<h1>My name is sandeep</h1>

<p>blah blah blah <a href="http://www.google.com">dudududu</a> blah blah blah </p>

<script type="text/javascript">
if (document.getElementsByTagName="h1" & document.body.innerHTML.match("My name is sandeep"))
{
alert("its there")
}
else (document.getElementsByTagName="h1")
{
document.write("I am someone else")
}
</script>
</body>


k, as you can see here, i have text in heading1 (h1) saying 'my name is sandeep'. I want to be able to externally edit this text using javascript. I have been told that this is possible using innerhtml. I wasn't really sure what i was doing, but i thought i might need to get elements with the tag name of h1, and then use some sort of innerhtml replacing code.

however, it has to be conditional, so that if the 'My name is sandeep' text does not appear on the page, then nothing happens.

The results were left simple for now, if it was true then i tried to do it so it made a quick popup, and if it was wrong then it just says that its someone else...i want to have more javascript functions, but im sure that i can manage to do that after the rest is sorted.

If ne1 can help me with this, then i will be very thankful, bin struggling on this for a bout a week now, simply cos im a n00b to javascript programming like this.


thanks for readin, and helpin if you can
sandeep

brandonH
06-21-2005, 08:48 PM
try setting an id to the <h1> like so: <h1 id=h1></h1>

that way you can use document.getElementById('h1')
i only say this because you may elect to use the <h1> tag more than once (or at last some people do). this way there is a specific id related to the tag.


<body>
<h1 id=h1>My name is sandeep</h1>

<p>blah blah blah <a href="http://www.google.com">dudududu</a> blah blah blah </p>

<script type="text/javascript">
if (document.getElementById('h1') && document.getElementById('h1').innerHTML=="My name is sandeep"){
alert("its there")
}
else{
document.write("I am someone else")
// document.getElementById('h1').innerHTML='I am Someone else';
}
</script>
</body>

also, this wont work if there is no <h1 id=h1> tag.

Kor
06-22-2005, 10:49 AM
As far as I can see, you want rather search for a certain string inside certain tags. If so, use a dynamic DOM method to check all the h1 tags of the document, something like this:

<script type="text/javascript">
var myvar = 'My name is sandeep';
function checkText(){
var oH = document.getElementsByTagName('h1');//the collection of H1 tags
for(var i=0;i<oH.length;i++){
if(oH[i].firstChild.data.indexOf(myvar)!=-1){
....do something with the variable myvar....
break;
}
}
}
</script>

You don't need to have a certain id in that tag....

sandeep89
06-22-2005, 05:29 PM
y'all are great...used brandon's example, seems to wrk great...cheers!!!

bin trying that for tyme, didn't think that one lil mod wud sort it, bt cheers!