0

i do not know much about javascript searched long, but didn't get the reslut i need.

i want to replace on page load this

<p>---SOMERANDOMTEXT:::</p>

with

<strong>SOMERANDOMTEXT</strong>

played with this an many other snippets..

<script type="text/javascript">
    window.onload = function() {
        function myscript() {
            input = '---';
            output='New Text';
            document.body.innerHTML = document.body.innerHTML.replace(input,output);
        }
    }
</script>
4
  • So anything between "---" and ":::"? Commented May 13, 2014 at 8:40
  • 2
    Where does <p>---SOMERANDOMTEXT:::</p> come from? Do you produce that yourself? If so, there's a better way of doing what you're doing. Commented May 13, 2014 at 8:40
  • You don't need to wrap a function declaration like that in a window.onload. Commented May 13, 2014 at 8:41
  • Take a look in this post stackoverflow.com/questions/13389751/… Commented May 13, 2014 at 8:43

4 Answers 4

1

Here is the fast and fool proof way of replacing <p> tags with <strong> tags:

var ps = document.getElementsByTagName('p');
for (var i = ps.length; i--;) {
    var strong = document.createElement('strong'),
        p = ps[i];

    while (p.firstChild) {
        strong.appendChild(p.firstChild);
    }

    p.parentNode.insertBefore(strong, p);
    p.parentNode.removeChild(p);
}

If you need to change the text accordingly, place something like that in the while loop:

if (p.firstChild.nodeType === 3) {
    p.firstChild.nodeValue = p.firstChild.nodeValue.replace(/[-:]{3}/g, '');
}

DEMO: http://jsfiddle.net/5m9Qm/1/

Sign up to request clarification or add additional context in comments.

1 Comment

@oGeez Added the workaround which will work for all direct children of each <p> tag.
0

You need to call your myscript function like this -

window.onload = function() {
  myscript();
}
function myscript() {
    var input = '---';
    var output='New Text';
    document.body.innerHTML = document.body.innerHTML.replace(input,output);
}

Comments

0

You are declaring a function myscript but never invoking it. Try this (untested) code:

<script>
window.onload = function(){
    var p = document.querySelector('p');
    var strong = document.createElement('strong');
    strong.innerHTML = 'replaced';
    p.parentNode.replaceChild(strong,p);

})
</script>

Note this requires modern browsers.

Comments

0

You can use a regex.

Try:

output = document.body.innerText.replace(/[-:]{3}/g, '');
document.body.innerHTML = document.body.innerHTML.replace(/<p>---.*:::<\/p>/, '<strong>' + output + '</strong>');

DEMO

4 Comments

Or /<p>---.*:::<\/p>/ so that you replace the paragraph, rather than append to it.
@oGeez Actually i havent cleared yet what OP wants to do, but good point. I edited
This solution will remove all bound events in body + it is tremendously slow.
Also, I've just realised, this would be you'd need to specify a new value, which defeats the idea (AFAIA).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.