Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2009
    Location
    Hamphire, UK
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post

    Finding specific HTML and replacing it

    I'm a bit of a Javascript noob, so please bear with me!

    I'm trying to create a piece of code that will check a webpage and replace any instances of a specific line of HTML with another line of HTML.

    The code I have so far doesn't work, but I have a feeling that's down to my awful Javascript skills!

    For example, in the following code I'd want to replace all instances of <li><a href="index.pdf">Index</a></li> with <li><a href="contents.pdf">Contents</a></li>

    Code:
    <div id="downloads">
        <p>Please click on your downloads below:</p>
        <ul>
            <li><a href="index.pdf">Index</a></li>
            <li><a href="chapter1.pdf">Chapter 1</a></li>
            <li><a href="chapter2.pdf">Chapter 2</a></li>
            <li><a href="chapter3.pdf">Chapter 3</a></li>
            <li><a href="credits.pdf">Credits</a></li>
        </ul>
    </div>
    The Javascript I have so far is:

    Code:
    <script type="text/javascript">
    
    var aEls = document.getElementsByTagName('a');
    for (var i = 0, aEl; aEl = aEls[i]; i++) {
    	aEl.href = aEl.href.replace('index.pdf','contents.pdf');
    			
    }
    Obviously this is meant to just replaces the a href elements, but I got a bit stuck after this!

    Any pointers would be much appriciated.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Obviously this is meant to just replaces the a href elements, but I got a bit stuck after this!
    What if there's an item like
    Code:
    <li><a href="foo.pdf">Index</a></li>
    Edit: You might need something like

    Code:
    var aEls = document.getElementById('downloads').getElementsByTagName('a');
    	for (var i = 0; i<aEls.length; i++) {
    		//alert(aEls[i].href.indexOf('index.pdf'));
    		if(aEls[i].href.indexOf('index.pdf')!=-1){
    			aEls[i].href = aEls[i].href.replace('index.pdf','contents.pdf');
    			aEls[i].innerHTML='Contents';
    		}
    				
    	}

    ?
    Last edited by abduraooft; 06-11-2010 at 11:41 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jun 2009
    Location
    Hamphire, UK
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post
    Actually, I've just realised what I need is slightly different to my original post.

    The page this code will go on dynamically generates a number of pieces of text based on the user's answers to a survey, which I want to then replace with an appropriate pdf download.

    So the page will generate something like:

    Code:
            <div id="downloads">
    			<p>You expressed an interesting in the following chapters. Please click on your downloads below:</p>
                <ul>
                    <li><p>Index</p></li>
                    <li><p>Chapter 2</p></li>
                    <li><p>Chapter 3</p></li>
                </ul>
            </div>
    And I want the page to automatically replace the chapters with the appropriate <a href="index.pdf>Index</a> for example.


    Hope that's clear!


  •  

    Posting Permissions

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