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 4 of 4
  • Thread Tools
  • Rate This Thread
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    64
    Thanks
    19
    Thanked 0 Times in 0 Posts

    How to replace an element with a completely different?

    Assume I have the following code:

    <div class"foobar">
    ....
    <a class="aaa" .....> .... </a>
    ....
    </div>

    Now I want to completely replace the <a> element (including child stuff) by another.
    I have no access to the original html code but want to modify it afterwards by Greasemonkey script.

    However the following does not work:

    var address = document.URL;
    var div = document.getElementsByClassName("foobar.aaa")[0];
    div = "<a href=\"" + address + "\">" + address + "</a>";

    How else does that work?

    Peter

  2. #2
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    365
    Thanks
    1
    Thanked 50 Times in 47 Posts
    You've got a LOT of misunderstandings in there. GetElementsByClassName only works off ONE class, not multiples, so "foobar.aaa" is gibberish. Assigning markup to a variable does NOT create a DOM element, which is what a getElements would return.

    ... and really you shouldn't be screwing around with markup in the scripting to begin with; the two ways of doing that -- document.write and innerHTML -- both have a slew of problems which is why their use is generally frowned upon.

    To get all classes in foobar you'd have to get foobar FIRST. THEN you'd need to get all the AAA inside it... or in your case if you just want the first one, index [0]... To replace that anchor with another anchor, you need to make it as a DOM element, populate it with a value and properties, then use replaceChild on the parent.

    THIS:
    Code:
    var
    	foobar = document.getElementsByClassName('foobar')[0],
    	aaa = foobar.getElementsByClassName('aaa')[0],
    	newAaa = document.createElement('a');
    
    newAaa.appendChild(document.createTextNode(address));
    newAaa.href = address;
    foobar.replaceChild(newAaa, aaa);
    Is what I THINK you are trying to do... replace the first class="aaa" in the first class="foobar"

    Generally speaking in this day and age if you are building markup as a string in JavaScript, you're doing something wrong.
    From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
    http://www.cutcodedown.com

  3. #3
    New Coder
    Join Date
    Dec 2011
    Posts
    64
    Thanks
    19
    Thanked 0 Times in 0 Posts
    @deathshadow: Very interesting. thank you.

    However I am searching for ways to reduce the number of all these statements.

    Assume that element a with class="aaa" is unique below element div with class="foobar".

    1.) Can I merge somehow the first two commands like in:
    oldelem = document.getElementsByClassName('foobar a.aaa')[0];
    2.) Furthermore: Is there a way to replace an arbitrary element rather than replace a child. I would appreciate to write:

    replaceElement(newelem, oldelem);
    3.) At last I would ease the creation of a new element. Is it somehow possible to write

    var address = document.URL;
    newelem = document.createElement("<a href=\"" + address + "\">" + address + "</a>");
    The browser engine should interpret whats passed inside the brackets. Its simply the same as if the browser would get the code from the original server:
    It has to interpret and generate a corresponding DOM (sub)tree. So it should be possible to do it on-the-fly later again.

    Does it work?

    Thank you again
    Peter

  4. #4
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    Quote Originally Posted by pstein View Post

    However I am searching for ways to reduce the number of all these statements.

    Assume that element a with class="aaa" is unique below element div with class="foobar".

    1.) Can I merge somehow the first two commands like inldelem = document.getElementsByClassName('foobar a.aaa')[0];
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>
    <div class=foobar>
    <div class=aaa>hiyas</div>
    </div>
    <script>
    alert(document.querySelectorAll('.foobar .aaa')[0].innerHTML)
    </script>
    </body>
    </html>

    Quote Originally Posted by pstein View Post


    2.) Furthermore: Is there a way to replace an arbitrary element rather than replace a child. I would appreciate to write:

    replaceElement(newelem, oldelem);
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>
    <div class=foobar>
    <div class=aaa>hiyas</div>
    </div>
    <script>

    replaceElement(document.createElement('hr'), document.querySelectorAll('.foobar .aaa')[0])
    function replaceElement(newEle, oldEle){
    oldEle.parentNode.replaceChild(newEle, oldEle)
    }
    </script>
    </body>
    </html>


    Peter
    [/QUOTE]
    Cleverness is serviceable for
    everything,
    sufficient in nothing.


 

Tags for this Thread

Posting Permissions

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