0

Seemingly simple but I can't find the correct placement. I have the following function in my HTML head:

<script>
(function($) {
    $.fn.writeText = function(content) {
        var contentArray = content,
            current = 0,
            elem = this;
        setInterval(function() {
            if(current < contentArray.length) {
                elem.text(elem.text() + contentArray[current++]);                
            }
        }, 1000);
    };    
})(jQuery); 
</script>

I call it in the body like:

<script>
// test getArray() method in external JS file
document.write(getArray());
$(document).ready(function($){      
    var contentArray = getArray();
    $('#calculations').writeText(contentArray);
}); 
</script>
<h3>Fibonacci Sequence:</h3>
<p id='calculations'></p>   

I can't find out where to place the '+ br /' to concatenate each writeText with a line break.

4
  • Not sure I get it, jQuery's text() replaces the content, it does not add lines, is that the issue you're having ? Commented Oct 2, 2014 at 19:16
  • Not sure what you are trying to accomplish, but I am certain that how you are doing it is way more complicated than necessary. Could you please describe what you want the expected behavior to be? Commented Oct 2, 2014 at 19:24
  • @adeneo I'm taking an array and displaying each element of the array on a timed interval on a new line. I have a function that cycles through the array displaying each array element on the same line but I can't figure out where to put the line break to accomplish my overall goal. Commented Oct 2, 2014 at 21:13
  • @protonfish I'm taking an array and displaying each element of the array on a timed interval on a new line. I have a function that cycles through the array displaying each array element on the same line but I can't figure out where to put the line break to accomplish my overall goal. Commented Oct 3, 2014 at 18:44

2 Answers 2

2

Using line breaks is a little amateur. Here's a script that appends paragraph tags.

$(function() {      
    var contentArray = ['first', 'second', 'third'],
        iContent = 0;

    function showContent() {
        $('#calculations').append('<p>' + contentArray[iContent] + '</p>');
        if (iContent < contentArray.length - 1) {
            window.setTimeout(function () {showContent(); }, 1000);
            iContent = iContent + 1;
        }
    }

    window.setTimeout(function () {showContent(); }, 1000);
});

Also you don't need

$(document).ready(

A simple $( will do the same. Why are you doing a document.write() in your code? If you just want to see the array values a console.log() would work better.

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

1 Comment

The write() was a test to verify the array being pulled from my external JS file. As I'm sure you can tell, being new to JS, I was trying to step through the code to check for breakpoints like I would have done in VS for .NET projects. I'll note the cosole.log() for future reference, thank you.
0

I am taking a wild stab at what I think you are trying to do, but if I am correct, wouldn't this work?

$('#calculations').html(getArray().join('<br>'));

I would also not put JS in the head of your doc. Put it at the end the the body tag instead.

Comments

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.