Jump to content


Photo

GetElementBy


  • Please log in to reply
4 replies to this topic

#1 FrOzeN

FrOzeN
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 21 September 2006 - 12:29 PM

I'm trying to modify some properties with JavaScript by using there class name. Can anyone tell me what's wrong with the following example, and/or an alternative to do it correctly?

<html>
<head>
<title></title>
</head>
<body>

<span class="classname">Test</span>

(script type="text/javascript">
  document.getElementByClass("classname").innerHTML = "Example worked!";
</script>

</body>
</html>

Thanks.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 September 2006 - 12:43 PM

there is no function getElementByClass unless you have written it yourself. you've got to remember that several elements can hold the same class. you can either use getElementById and do it with an ID if you only have one element you're wanting to return, or else, you can cycle through all a specific type of element with the class you're looking for like this:
elements = document.getElementsByTagName("SPAN");
for (i = 0; i < elements.length; i++) {
  if (elements[i].className == 'classname') {
    // do your thing here
    elements[i].innerHTML = "Example worked!";
  }
}

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 21 September 2006 - 01:17 PM

And if you're going to be doing that repeatedly throughout the page, you should cache the results.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 September 2006 - 01:20 PM

And if you're going to be doing that repeatedly throughout the page, you should cache the results.


good point, once again. boy, i'm glad you're here to help clean up after me ;)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 FrOzeN

FrOzeN
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 22 September 2006 - 04:45 AM

Thanks, that's what I was looking for.

Sorry about the post, my router is setup to cut me off at 10:30 pm weeknights so I don't spend all night on the net, and I rushed to post it before it cut me off. When I clicked post some error occurred when posting '<' and '>' so I quickly changed them to brackets figuring you guys could make sense of what I meant and accidently duped half the post when copying + pasting in a rush.

[EDIT] I just cleaned up the post and found the bug that stopped it from posting correctly the first time. When trying to post the line below it firefox returns the error, "The connection to the server was reset while the page was loading."

<script type="text/javascript">

Note: I've used a BBCode trick to avoid the error by adding "[b][/b]" within the line.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users