SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how to get css and the html into JS function?

    how do I get the css and the html into the function?

    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function myFunction()
    {
    return ("Hello world!");
    }
    </script>
    </head>
    <body>
    <div style="float:right;">
    <script type="text/javascript">
    document.write(myFunction())
    </script>
    </div>
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    U can do like:

    <html>
    <head>
    <script>
    function m()
    {
    y=document.getElementById("linhpro");
    alert(y.innerHTML);
    }
    </script>
    </head>
    <body onload=m()>
    <h1 id="linhpro">Hello word</h1>
    </body>
    </html>

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but what about the css? Isn't there an advantage to getting both into the javascript?

  4. #4
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hy,
    Try test and understand this code:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function myFunction(anid)
    {
      var cssatr = document.getElementById(anid).getAttribute('style');   // get the value of "style"
      var htmlanid = document.getElementById(anid).innerHTML;        // get html code
      return ('css style = '+ cssatr+ '<br>html = '+ htmlanid);
    }
    </script>
    </head>
    <body>
    <div id="anid" style="float:right;">
    Some html code
    </div>
    <script type="text/javascript">
    document.write(myFunction('anid'))
    </script>
    </body>
    </html>
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I understand that. Your use of getAttribute lets me ask this question.

    Now, can you work createElement into your script?

    I think the two can work together, but I don't know how.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How close is this to display a div ? Admittedly, I'm guessing at the use of createElement() and setAttribute().
    HTML Code:
    <html>
      <head>
        <script type="text/javascript">
            var div = document.createElement('div');
            div.style.display = 'absolute';
            div.style.left = '500px';
            div.style.top = '200px';
            div.style.width = '100px';
            div.style.height = '100px';
            div.style.backgroundColor = 'red';
            div.setAttribute('id', 'link-container');
        </script>
      </head>
    <body>
      <div id="link-container"> 
      <script type="text/javascript">
         document.write(div);
      </script>
      </div>
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •