Using AddEventListener Or Predefined Events (OnClick / OnLoad)

May 12, 2010

I have been reading and practicing Javascript for the last month and so far I'm happy with it. I noticed that you can add Event Listeners and trigger a function based on that event but I also noticed that you could add an event directly to any element as an attribute, something like:

Code:
<p onclick="doSomething()">Click Me</p>
function doSomething(){
//do something
}

So my question is why would someone add and event listener instead of adding that event directly in the element (as the sample above)? The reason I'm asking is because adding event listeners involves more code:
Code:
var elementName= document.getElementById('elemenstsID');
elementName.addEventListener("click", doSomething, false);

I guess what I don't know understand is why would someone choose to add an event listener instead, I know it is more OOP but doesn't the "onclick","onload" etc., do the same thing?

View 4 Replies


ADVERTISEMENT

Executing Predefined Function Via OnClick

Aug 3, 2011

I'm an experienced programmer (c, perl , python, bash) but absolutely newcomer with Javascript. The code I'm experimenting with is shown below. What I'm trying to do at this point is define a function in a script section in the head of my html page, then call that function in the onclick action of a form button. Sounds trivially simple, but for some reason it isn't doing what I expect. The eventual result will be that the Javascript will launch a Silverlight video player using the values supplied by the end user, but for now I just want to get the basic buttons to work.

<code>
<html>
<head>
<title>Silverlight player</title>
<script type="text/javascript">
var vProto;
var vServer;
var vStream;

function showMe() {
alert("In the function");
vProto = document.getElementById('proto').value;
vServer = document.getElementById('server').value;
vStream = document.getElementById('stream').value;
return false;
}

function alertMe() {
alert(document.getElementById('server').value);
return false;
} .....

View 1 Replies View Related

Run Conditional Statements On Events Such As "onclick" Or "onload"?

Oct 6, 2011

I have created a bunch of methods for a shopping cart I am implementing on the website,and at the same time I take information from my database to get the quantity of how much stock I have left.Basically I want to do a bit of error handling, so that if you select 5 things to go into the shopping cart but the amount of items that are in stock is only 3 then an error message will come up saying "Sorry! Don't have that many items in stock!". I have a setCookie method which I created, and it is triggered by an onclick event.

I guess what I am trying to say is... Is it possible to run conditional statements on events such as "onclick" or "onload"? As it currently stands, I am going to have to do the error handling within my external js file and that really seems sloppy to me since I really want that external file to stick to cookie creation. I would really prefer to do my conditionals within my view/html file =/

View 2 Replies View Related

2 Onload Events?

Mar 21, 2001

How would I write 2 onload events into the body tag?

I currently use:

onLoad="if (self != top) top.location=self.location" and have found a scroller that may work better than my current one but it has an onload command as well.

I can't seem to figure out how to do both.

What about if I have 3?

View 7 Replies View Related

Have Two Onload Events At The Same Time

Oct 30, 2009

I am working on my website and I have it coded to rotate my header banner from one to the other at onload but I would like to run a running scroll in my task bar also. I know both the rotating headers and scrolling called the function from the body tag at on load. Individually they both work but not together.

Here is what code I am using at the on load.

<body onload="var changeImages=setInterval('propertyAd()',7000);" onload="setInterval('scrollStatusBar()', 100)"; >

View 1 Replies View Related

Multiple Onload Events In The Body Tag

Jun 22, 2007

Is it possible to load multiple onload events in the body tag?

View 6 Replies View Related

Having Multiple OnLoad Events On A Given Page?

Feb 13, 2009

Any problem with having multiple onLoad events on a given page? Does that cause any sort of conflict?

[Code]....

View 4 Replies View Related

Javascript Events In Head... Window.onload

Nov 4, 2006

Okay you know how you can do something like...

window.onload=function(){myFunction();}

in order to contain all you javascript in the head of your document??

Well why can't I seem to do...

document.getelementbyid('MyElement').onclick=function(){myFunction();}

??

View 13 Replies View Related

JQuery :: Wire Up Plug-ins For DOM Events Without Using A Global Onload Event?

May 25, 2011

I have a dinky ajaxSubmit plugin which causes a form submit action to submit that form via AJAX along w/ some validation. Currently, in a global.js file which is included by every page, I have:

$(function() {
$("form.ajaxsubmit").ajaxSubmti();
});

Sort of a global onload event. The plugin uses the live() method to bind so that future forms added via any callbacks would be wired up as well. Question is: is this a best practice w/ jQuery? Is there a better way to wire up plug-ins for DOM events w/o using this sorta global onload event?

View 1 Replies View Related

Multiple Onload Events - Doesn't InitThree Overwrite The Previous?

Apr 24, 2010

It's from this article here and it appears to be a classic script that's wildly used. [URL]

[Code]...

It makes sense that initOne & InitTwo get called at the same time.. but why doesn't initThree overwrite the previous? And it's strange to me that oldOnload(); is initOne the first time arround. the second time it's == to the functions it's now contained in.

View 1 Replies View Related

Multiple Onload Event - Utilize Two Separate Onloads Events

Aug 10, 2009

I am new to Javascript and am trying to add some functionality to a website but have come across a problem when trying to utilise two seperate Javascript onloads events. I have a text scroller to display the latest news and I also have a javascript lightbox gallery. Both of these events work perfectly on their own but when I try to include both on one page only one of the events is executed. My code is displayed below:

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
<script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script>
<script type="text/javascript" src="js/jquery.li-scroller.1.0.js"></script>
[Code]....

View 4 Replies View Related

Possible To Add Onclick Events To IFrames?

May 9, 2010

Is it possible to add an onclick event to an iframe or perhaps a DIV that holds an iframe?

In specific I am using the Facebook Open Graph like button code...

What I would like to do is add a simple onclick event to it so that I can run a process when the user clicks on it, at the moment I am just trying alert but cannot get it to work. By setting the iframe within a DIV with height/width specs set would an onclick event work within the DIV?

View 1 Replies View Related

Way To Have Two Onclick Events Within One Anchor Tag?

Jul 20, 2011

I'm filling in for a coworker on a radio stations website.

The station currently streams live online. I want to add an event tracking so I can track how many people are streaming. I'm really new to js, but I think I figured it out (keyword is "think"). However, there was already an onclick event within the anchor tag. Can I have two in the same tag? Is there a better way to do this?

View 1 Replies View Related

Two Events With One OnClick Of A Button

Sep 12, 2007

I have the following code which places multiple buttons on a page. I need 6 of the 7 buttons to perform two actions on one click. I have searched the web and can't quite comprehend the suggestions that I find. (I'm a newb). :confused:

Is it possible to just onClick=action1 & action2?

Here's the code. I seperated the head from the body to reduce the amount of scrolling....

View 2 Replies View Related

Merging 2 Onclick Events

Jun 29, 2009

I attached an onclick event to a link, however I have another one, that needs to be put on the same link, unfortunately I have no idea how to do it. how can I attach the function changecolor to the "abcdefgh" link?

View 1 Replies View Related

Multiple Events For Onclick

Jan 23, 2010

I have several pages, each having a button and a textbox. The button has an onclick event directly coded into the HTML markup.The textbox has a onchange event in a similar fashion.What I need to do is create a small javascript snippet which adds another function to the onclick event of the button, and the onchange event of the textbox.I am not allowed to make any changes to the HTML of the pages, or the functions which are called originally by the events.All I can do is place a small javascript file in the head section of the pages.

The code should attach another function which is called during the event, while at the same time preserving the current function attached to the event.

View 2 Replies View Related

Onload Set Onclick And Pass Argument?

Apr 8, 2010

i have an initAll() called on window.onload within initAll() I assign a few onclick handlers to some links. However, it would be great if I could pass an argument to the handler, so I could use the same handler every time... but if I include two brackets for each handler, it executes immediately... ignoring the 'onclick' bit...

window.onload = initAll;
function initAll(){
document.getElementById('btn_events').onclick = getSpecificFeed('events');
}

[code]....

View 2 Replies View Related

Events Get Lost (from OnBlur To OnClick)

Oct 5, 2005

*The Situation*
A traditional situation where HTML form inputs are checked...

(if simplified then it would look something like this)

<form onSubmit="return checkWholeForm(this)">
<!-- other inputs -->
<input type="text" name="anInput" onBlur="dataCheck(this, ...)" />
<span id="error_anInput"></span>
<!-- other inputs -->
<input type="submit" name="btnSubmit" />
</form>

dataCheck validates the inputs value and if something is wrong, then..
document.getElementById('error_anInput').innerHTML = 'Error!'
or if data is valid then the content of span is removed.

The checkWholeForm function iterates through all elements on the form
and triggers onblur() for each input... leading to executing function
dataCheck (and so changing innerHTML of some specific span elements if
needed).

*The Problem*
If I have entered incorrect data to an input and hit the submit button
with my mouse (causing the onBlur event to be triggered just right
before onSubmit) then *occasionally* for some fields the onSubmit event
is not triggered because the onClick event is not triggered. :S

As there are actually quite many complex functions (tested and these
seem OK afaik) that are doing the checks then dowes anyboudy have a clue
what type of code might break this. I thought at first that setting
innerHTML to some value during onBlur disables all waiting events but as
this is happening occasionally (on some machines) I'm in doubt...

any ideas what to check? double declaration of function/variable names?
Not deleting some object after usage? ... anything?

Waiting for any ideas...

PS. http://eix.lap.ee/test/portali_js.html in the example *occasionally*
third field generates the error - remove any content from the field
and stright hit the submit (*with mouse* - to create onBlur and onClick
at the same time).

View 2 Replies View Related

Onclick Events For Multiple Fields

May 30, 2009

I am working on a simple control panel where I have text input fields in which a user can click on a text box and the text will automatically focus and select. I have done this fine. However, when I use the same onclick event for multiple fields, only the last one created works. So, if I were to comment out the password and email field additions, then the userName field would work correctly. If I were to just comment out the email field, password would work correctly but userName would not. And finally, if I have the code as is, userName and password do not function correctly while email does.

View 7 Replies View Related

Changing Onclick Events On Buttons?

Jun 10, 2010

I hope that someone can help me with this strange problem I have here. I have some script which changes a button and changes the onclick event. The strange thing is that although it is changing it. All the events have the same parameter even though my code is giving each button's onclick event it's own unique parameter.

[Code]...

View 1 Replies View Related

Mouseover, Mouseout And Onclick Events

Jan 29, 2007

I am reading values from a database into a table and I have it so that when the mouse is over a row the row is highlighted blue and when the mouse exits that row the row becomes white again - straight forward.

each row also has a check box with an onclick event and ultimately I would like when the checkbox is checked the row to be highlighted red and remain that colour. The problem is that the mouseover and mouseout events still fire. Is there way to disable these when the box is checked? Code:

View 14 Replies View Related

How To Hide Status For All Onclick Events?

Nov 20, 2002

Is it possible to set the browser default to hidestatus for all onclick events?

View 3 Replies View Related

OnClick Or OnChange Events To SQL Record

Oct 5, 2011

Have an asp page that user selects a week from date picker and it returns data from sql table in a series of checkboxes for overtime availability. i.e., for the week of 10/10/2011 the checkboxes are Monday through Sunday, the user clicks the checkboxes they want to work overtime and hits a submit button. Let's assume they chose Monday and Wednesday. If they go back to this page later and pick the week of 10/10/2011 the sql table shows checked in the Monday and Wednesday checkboxes.

They can then make any changes they want, hit the submit button, and that sql record is updated with the new data. So on to the issue. For audit purposes, there is another sql table called ot_audit. What I want to do is every time one of the checkboxes is changed (pre hitting the submit button), I need a new record added to the ot_audit table. It will show a datetime stamp, who made the change, what was clicked, and if it was checked or unchecked. I'm new here and all I know is there are onChange and onClick events. Lost after that.

View 1 Replies View Related

Blur Function - Multiple OnClick Events?

Dec 29, 2009

I would like to open a window "under" the current window (So it doesn't open on top). I think this can be achieved using the .blur function.

So something like:
<a href="#" onclick="openWindow [URL];this.blur();return false;">Open website</a>
(Obviously this doesn't work)

I've seen ways to do it, if I'm using 1 url, but because I have a list of 30 different links, I can't simply use:
Code:
<SCRIPT LANGUAGE="JavaScript">
function goNewWin() {
TheNewWin =window.open("somepage.html");
TheNewWin.blur();
}
</SCRIPT>

Which is why I need something that I can use on each different url individually that opens a new window under the current one.

View 13 Replies View Related

Attach Onclick And Onchange Events To All Input Fields

May 24, 2010

I am trying to create a script that onload will attach an onclick and onchange event to all fields.I have also tried using setAttribute instead of attachEvent and it still does not work.Basically what the events do is disable a interval when an input field is selected and enable the interval when it is no longer selected.

View 6 Replies View Related

JQuery :: Retrieving Nested XML Nodes And Binding To OnClick Events

Jan 26, 2010

I have created two onClick events that i need to combine into one with jQuery. I have an unordered list:

Code:

<ul id="coverTabs">
<li class="currentTab"><a href="#">Individual</a></li>
<li><a href="#">Couple</a></li>

[code].....

So the active tab is set in the HTML to Individual by default.The following jQuery sets the active depending on which one is clicked.

Code:

$('ul#coverTabs > li').live('click', function() {
// Clickable tabs
// Removes default class applied in HTML and onClick adds 'currentTab' class

[code]....

what i want to achieve is when a user clicks a tab we call the XML document. The appropriate tab name is fetched and subsequently when a user clicks on a column under that tab the appropriate level cost data is displayed on the page.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved