Forum Home
Press F1
 
Thread ID: 116401 2011-03-02 09:24:00 javascript syntax question Tony (4941) Press F1
Post ID Timestamp Content User
1183017 2011-03-02 09:24:00 I have a bit of HTML:

<p name="MyText">Foo</p>

How do I change "Foo" to "Bar" as the result of the OnClick on another element?

I'm only a novice at Javascript, and I can't work out how to do it. :badpc:
Tony (4941)
1183018 2011-03-02 19:28:00 OK, I've managed to do it with an image:



function MyFunc ()
{
document.Pic.src="new path to image";
}

<img name="Pic" onclick="MyFunc" src="original image path">



It seems to me I ought to be able to do something like


document.MyText.something="Bar";


but I haven't discovered how to do that.
Tony (4941)
1183019 2011-03-02 20:19:00 You'll need to give the text element an ID, and then set it's innerHTML value, e.g.:


<script type="text/javascript">
function changeText(){
document.getElementById('changeme').innerHTML = 'New Text';
}
</script>
<p>Here is some <b id='changeme'>Old Text</b> </p>
<input type='button' onclick='changeText()' value='Change Text'/>
inphinity (7274)
1183020 2011-03-02 20:33:00 Hah! I was about to post that I had found the solution when you just beat me to it. I finally managed to form the correct google query to get the answer I needed. Thanks for the help. That's one of the problems with basically learning javascript via google - knowing what the right question is! Tony (4941)
1