/    Sign up×
Community /Pin to ProfileBookmark

Question about DIV and JavaScript

I have this line of code:

<div class=”blue” onclick=”myfunction()” id=”blue2″>

This works fine and makes the JavaScript work if you click the Div.

Is there a way to make it so you don’t have to click it, but it works automatically upon page load?

Thanks.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@cootheadJan 16.2013 — Hi there davidwhite,

does this help...
[color=navy]
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta name="language" content="english"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;

&lt;title&gt;basic template&lt;/title&gt;

&lt;style type="text/css"&gt;

&lt;/style&gt;

&lt;script type="text/javascript"&gt;
(function() {
'use strict';

function [color=red]myfunction() [/color]{

}

window.addEventListener?
window.addEventListener('load',[color=red]myfunction[/color],false):
window.attachEvent('onload',[color=red]myfunction[/color]);

})();

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div class="blue" id="blue2"&gt;

&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;[/color]


[i]coothead[/i]
Copy linkTweet thisAlerts:
@PadonakJan 16.2013 — ... or just

&lt;body onload="document.getElementById('blue2').click()"&gt;
Copy linkTweet thisAlerts:
@cootheadJan 16.2013 — Hi there Padonak,
[color=navy]... or just....[/color][/quote]

I would suggest not. ?

[b][u]Further reading[/u][/b]:-
[list]
  • [*][url=http://en.wikipedia.org/wiki/Unobtrusive_JavaScript#Separation_of_behavior_from_markup]Separation of behavior from markup[/url]
  • [/list]


    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @PadonakJan 16.2013 — hello coothead )

    of course you are right, but we can use such things sometimes obtrusive or not for example when debugging smth or else ?
    Copy linkTweet thisAlerts:
    @cootheadJan 16.2013 — Hi there Padonak,

    You should also note that your example, unfortunately, does not seem to work in Safari 5.1.7. :eek:

    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @PadonakJan 16.2013 — <i>
    </i>&lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;

    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    &lt;meta name="language" content="english"&gt;
    &lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
    &lt;title&gt;basic template&lt;/title&gt;

    &lt;style type="text/css"&gt;

    &lt;/style&gt;

    &lt;script type="text/javascript"&gt;
    function xx(){
    var divs=document.getElementsByTagName('div');
    for(var i in divs){divs[i].onclick=function(){alert(this.id||'no id defined! innerHTML = '+this.innerHTML);}}
    }
    &lt;/script&gt;

    &lt;/head&gt;
    &lt;body onload="xx();document.getElementById('blue2').click()"&gt;

    &lt;div class="blue" id="blue2"&gt;
    lol
    &lt;/div&gt;
    &lt;div class="blue"&gt;
    megalol
    &lt;/div&gt;

    &lt;/body&gt;
    &lt;/html&gt;


    IE,FF,Chrome and Opera [B]VS[/B] Safari

    4:1 Safari loose ))
    ×

    Success!

    Help @davidwhite spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.7,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...