Results 1 to 4 of 4

Thread: innerHTML with Classes

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default innerHTML with Classes

    I have to change the innerHTML of a span with a class rather than an id. The reason I can't attach an id to it is it's part of code automatically generated by Joomla. Thus, I do not have direct access to it.

    I wrote a quick script to do this, using an onload function on <body>. Here's the code:

    Code:
    function changeRecBlogTitle(){
    	var spans = getElementsByTagName("span");
    	for (i = 0; i < spans.length; i++){
    		if spans[i].className = "separator"{
    			inner = spans[i].innerHTML;
    			if(inner == "Blog"){
    				spans[i].innerHTML = "Recent Blogs";
    			}
    		}
    	}
    }
    And, I call it:

    HTML Code:
    <body onload="changeRecBlogTitle();">
    Finally, here is the <span>:

    HTML Code:
    <span class="separator">Blog</span>
    I simple want to change "Blog" to "Recent Blogs." Any idea what I'm doing wrong?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Oct 2009
    Posts
    845
    Thanks
    14
    Thanked 189 Times in 188 Posts

    Default

    Hi regular javascript is to complicated for me but if you have jquery on your page already something like this might work
    Code:
    <script type="text/javascript">
    $(document).ready(function(){ 
    var el = $('.separator');
    el.html(el.html().replace(/Blog/ig, "Recent blogs"));
    });
    </script>

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I believe mootools already comes with Joomla, and do not want to run into conflicts between mootools and jquery by adding jquery now. Do you know how to write the same code in mootools, or does anyone else know how to simply write it out in plain javascript? Thanks.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Code:
    function changeRecBlogTitle(){
    	var spans = document.getElementsByTagName("span");
    	for (i = 0; i < spans.length; i++){
    		if (spans[i].className == "separator"){
    			spans[i].innerHTML = "separator"}
    			else spans[i].innerHTML = "non-separator";
    		}
    	}
    Arie Molendijk
    Last edited by molendijk; 06-11-2010 at 11:34 PM. Reason: Correction

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •