how to embed HTML code into the javacript codes ?

Discussion in 'JavaScript' started by a_fazlani, Oct 18, 2009.

  1. #1
    when i want use some html codes into the some scripts, then it doesn't work or visitor just see source of HTML that i inserted! no its effect ... :(

    sample problem:
    <script type="text/javascript">
    		            var sayHello = new LiveValidation('sayHello', { validMessage: '<br/><a href="index.html"><img src="hello.jpg" border="0" valign="bottom"></a>', wait: 500});
    		            sayHello.add(Validate.Presence, {failureMessage: "test"});
    		            sayHello.add(Validate.Format, {pattern: /^hello$/i, failureMessage: "test" } );
    </script>
    Code (markup):
    do i need use document.write to cover HTML codes ? then its working ?
     
    a_fazlani, Oct 18, 2009 IP
  2. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't know where your "sayHello" object comes from, so i'd say you should use document.write.
     
    ThomasTwen, Oct 19, 2009 IP
  3. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #3
    The proper way of adding HTML is via the DOM. Here's how you would insert an image:

    var image = document.createElement( 'img' );
    image.src = 'http://www.imagelink.com';
    image.setAttribute( 'width', '100px' );
    image.setAttribute( 'height', '100px' );
    document.body.appendChild( image );
    Code (markup):
    The quickest way, but will write your HTML wherever your script is located on the page, is using:

    document.write( '<p>here is some html</p>' );
    Code (markup):
    You can also use innerHTML:

    document.getElementById( 'my_div' ).innerHTML = '<p>here is some html</p>';
    Code (markup):
     
    DollaradeConner, Oct 20, 2009 IP
  4. ajaXpert

    ajaXpert Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    If you write many dom elements use innerHTML property, or you can use dom node manipulation for a single element.
     
    ajaXpert, Dec 4, 2009 IP