Very easy&quick Javascript question.

Posted on 2011-05-13
Last Modified: 2012-05-11
How to achieve this php method of adding more to the same variable in javascript?

$variable = ''
$variable.= '<h1>This is a header</h1>'
$variable.= '<p>This is a paragraph</p>' 

Open in new window

Thank in advance.
Question by:idusty
    LVL 9

    Expert Comment

    you can use shorthand operator x += y
    LVL 9

    Accepted Solution

    var variable = ""
    variable += "<h1>This is a header</h1>"
    variable += "<p>This is a paragraph</p>"

    Open in new window

    LVL 9

    Assisted Solution

    there are two ways.

    one would be:
    var foo = "Hello";
    foo += " World!";

    Open in new window

    and the other one would be:

    var foo = "Hello";
    foo = foo.concat(" World!");

    Open in new window

    though the bottom one doesnt work in IE but would be faster.
    LVL 9

    Expert Comment

    sorry i missed  semi colon (;)

    var variable = "";
        variable += "<h1>This is a header</h1>";
        variable += "<p>This is a paragraph</p>";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now