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 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    64
    Thanks
    19
    Thanked 0 Times in 0 Posts

    replace a (partial) value of an attribute by another value? jQuery possible

    Assume I have an element like:

    <div class="aaa" myattribute="foobar1111" .....>....</a>

    The classname "aaa" is unique.

    1.) How can I replace "foobar1111" completely (!) by "dummy2222" from javascript/jQuery?

    2.) How can I replace ONLY "1111" inside the attribute by "2222" AND keep the remaining (possible unknown) text inside the attribute value whatever it is?

    If it is more comfortable jQuery could be used.

    Peter

  2. #2
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    <!doctype html>
    <html>
    <head>
    </head>
    <body>
    <div class="aaa" myattribute="foobar1111" >....</div>
    <footer style="position:fixed;bottom:0"><h1>FOOT</h1></footer>
    </body>
    <script>
    ele = document.getElementsByClassName("aaa")[0];
    alert(ele.getAttribute("myattribute"));
    ele.setAttribute("myattribute","dummy2222");
    alert(ele.getAttribute("myattribute"));
    ele.setAttribute("myattribute",ele.getAttribute("myattribute").replace("2222","3333"));
    alert(ele.getAttribute("myattribute"));
    </script>
    </html>
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  3. Users who have thanked DaveyErwin for this post:

    pstein (04-20-2016)


 

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
  •