Automatically click using onload? - javascript

How can I automatically click on an object using onload() with HTML.
Here's the button I would like to be automatically clicked:
<input type="button" value="{$LANG.checkout} »" class="checkout" onclick="addtocart();" />
What I would like to happen is for the button to be clicked automatically; as soon as a certain div is loaded. I know it's possible to do with Javascript, but is there any way to do it without javascript? If javascript is the only way I am more than open to it!

As a comment says, is not need to 'trigger a click automatically', just add the functionality in a script tag (or even better, in a separate file):
HTML
<input type="button" value="{$LANG.checkout} »" class="checkout" />
JS
<script>
addToCart();
function addToCart(){
//your code here...
};
</script>
Include this tag before the tag </body> (where body finish) and it will be execute when all your HTML (DOM) be ready.

Related

Inserting a big JavaScript in a html onlick Tag

I have a big JavaScript Code in a html file like
<script>CODE</script>
Now, this whole paragraph should only execute if the user clicks on a button (or something like that)
for example
<button id="MyButton" onclick= ???>MyButton</Button>
Thanks Guys :)
Wrap your code in a function, and specify that function in your button onclick.
<script>
function doSomething() {
CODE
}
</script>
<button id="MyButton" onclick="doSomething()">MyButton</button>

Button being sent automatically?

I need this button to be automatically "pressed" when entering the site, or after X seconds.
<button id='settingsTab' class='all'>||</button>
<div id='settingsContainer'>
<div id='channelInput' style='position:absolute;top:15px;font-size:0;width:330px;'>
<input type="text" id="channel" style='width:215px;' name="channel" value='sodapoppin'>
<button id='join'>Join</button>
</div>
</div>
Is this possible, having the "join" button being sent by just entering the site?
Also, im pretty new to this!
Is this possible, having the "join" button being sent by just entering the site?
yes it is possible.
You will have to use the setTimeout function of javascript or Jquery to achieve this. Also wrap your code in a document ready function so that it executes only when the document has been fully loaded. Make sure to put all your javascript/jquery code within a script tag.
Here is a sample code (in Jquery):
<script>
$(document).ready(function(){
setTimeout(function(){ $("button#join").click(); }, 2000); /* after 2 seconds */
});
</script>

Can external JavaScript access DOM elements from a different file?

Just started working in Dreamweaver recently. I was wondering if when you are working with external javascript files, do you have to pass in html elements or can the js file see their id? For example;
<body>
<script src="client.js"></script>
<input type="submit" name="submit" id="submit" onclick="getValue()" value="Submit"></td>
And then in the client.js file
function getValue() {
"use strict";
document.getElementById(submit).value = document.getElementById(otherelement).value;
}
This isn't working in the first place and I understand that there are other errors, but mainly - can the client.js file see and use getElementById(submit) and getElementById(otherelement)?
I would suggest shying away from using inline JavaScript elements, and doing things differently. I'd suggest using addEventListener() to bind events from JavaScript.
So, remove the onclick attribute, and just do:
<input type="submit" name="submit" id="submit" value="Submit">
We will be adding the event in JavaScript. For this to work, the script needs to be ran after the page (DOM) is loaded. You can use window.onload = function(){} to do this or you can load the script at the end of the page (before </body>).
Anyway, in your JavaScript, you want to use:
document.getElementById("submit").addEventListener('click', function(event){
// NOTE: You are clicking a submit button. After this function runs,
// then the form will be submitted. If you want to *stop* that, you can
// use the following:
// event.preventDefault();
// In here `this` will be the element that was clicked, the submit button
this.value = document.getElementById('otherelement').value;
});
document.getElementById( id ) takes id param as string
Use
document.getElementById("otherelement");
document.getElementById("submit");
also remove the </td> as there is no <tr> in your code
If you don't use quotes to wrap your strings, javascript will try to find variables named submit or otherelement. Try adding quotes like that :
function getValue() {
"use strict";
document.getElementById("submit").value = document.getElementById("otherelement").value;
}
If you have an HTML element with an id attribute, The JS engine automatically converts it to a variable..
e.g.
<input type="submit" name="submit" id="submit" onclick="getValue()" value="Submit"></td>
equals to the var submit in your JS code (considering you load your JS file when the DOM is fully rendered).
In every HTML page an element id is unique and that's why it is converted to a variable and wll not be overwritten until you decide so.
I was wondering if when you are working with external javascript
files, do you have to pass in html elements or can the js file see
their id
Yes you can see the ID:
function whoAmI(e) {
document.getElementById('output').textContent = e.target.id;
}
<button id='Hiii' onclick='whoAmI(event)'>Check ID</button>
<p id='output'></p>

Button not executing window.location

Im actually having a problem with html5 button in visualforce pages. I have a requirement where when i click on a button, it should redirect to a certain page (user do not want link). For some reason which i don't know, the function i'm executing do not work with HTML5 button (in fact the page only refresh but do not redirect) but when i use input of type button, the function is executed as expected.
I want to use the html5 button as there are some specific css already defined for button in the plugin im using. Find below codes for my button and javascript function :
<button class="butt" onclick="newContact()" >Nouveau</button>
<script type="text/javascript">
function newContact(){
window.location = "/apex/VFP01_NewContact";
}
</script>
What did I do wrong here, and what is the limitation of html5 button ?
Got to know the answer. In visualforce page, the html5 button execute a submit of my form. A way to prevent this, was to specify the attribute type="button".
You really have two options.
You could consider wrapping a element with an anchor tag that points to the URL that you want to target as seen below :
<!-- Target the "YourAction" action in your "YourController" controller -->
<a href='#Url.Action("YourAction","YourController")'>
<input type="Button" id="mybutton" name="url button" value="Next" />
</a>
or you could handle this purely through Javascript and perform your navigation that way :
<!-- The onclick event will trigger a Javascript call to navigate -->
<input type="Button" id="mybutton" name="url button" value="Next" onclick="window.location.href='#Url.Action("YourAction","YourController")';" />

Html click event not displaying the results (by using JavaScript) on the page

After clicking the button the JavaScript displays the result on the new tab, but I want the results to be displayed just below the button.
<html>
<body>
<input type="button" id="button1" value="Click Here" onclick="print();"/>
<script type="text/javascript"> //script to do some task
function print()
{
document.write('My Text Goes here ');
}
</script>
</body>
You can use document.write only before the pages complete to load.
If you want to add that text after the page is loaded you have to alter the DOM using some native javascript functions (appendChild, createElement, etc), or you can use jQuery (or other javascript framework) for this. The second is easier because you don't need to concern of browser compatibility.
jQuery eg:
$("#button1").after("My Text Goes here");
Remove semicolon from onclick function but it works without any problem when writing semicolon OR not.
<input type="button" id="button1" value="Click Here" onclick="print()"/>
<script type="text/javascript">
function print()
{
document.write('My Text Goes here ');
}
</script>

Categories

Resources