Call javascript on checkbox onclick - javascript

I don't understand what I'm doing wrong here. I just want my function to be called when I click the checkbox. Replacing the function call with alert() works, am I referencing my function incorrectly?
<html>
<head></head>
<body>
<script type="text/javascript">
function select(a){
document.getElementById("myDiv").innerHTML=""+a;
}
</script>
<input type="checkbox" onclick="select(1)">
<div id="myDiv">hi</div>
</body>
</html>
Thanks

Change the function name [e.g. selectFun]. select seems to be reserved keyword

This puzzled me as it looked ok to me too, So ran through the usual tests, eventually tried changing the function name and that worked fine.

Related

Onclick function not work in IE 11

I'm trying to add a submit function in my HTML and refer to a js file with my function. The code runs successfully in Chrome but not in IE11. The error message is "submitFunc(event) is undefined". Could anyone help me fix this problem? I have tried everything I can. T.T
function submitFunc(event) {
console.log("Hey what are you looking for? ")
}
<html>
<div>
<input class="btn btn-primary" type="submit" value="submit" onclick="submitFunc(event)">
</div>
</html>
<script type="text/javascript" src="/static/js/main.js"></script>
It might be that your <script> tag in outside of the <html>...</html> block. Try moving it up into the a <head>...</head> section just after the first <html> tag.
I don't know if you still need help, but you're loading the JavaScript file after setting up the on-click event. I would assume that means you don't have the function defined yet. I would move the "script" tag before the elements.

How to get the selected element in jQuery event delegation?

I've been trying to understand jQuery delegation by writing a short script, but I encountered 2 problems.
<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<input type="text" id="text" />
<div id="msg"></div>
<script>
function showMsg() {
if ($("#text").val() === "") {
$("#msg").html("Your input is empty");
} else {
$("#msg").html("You have entered something")
}
}
$("#text").on("blur", showMsg());
</script>
</body>
1). This event delegation doesn't work as expected, the message "Your input is empty" always shows itself indefinitely. How to fix this?
2). In the showMsg() function I have to explicitly use $("#text") for the script to work, if I use $(this) it won't work. What if I have a lot of input fields that need to use this function, is it possible to uniformly define the function so that those input fields can use it without having to change anything in the function?
All you need to do is change
$("#text").on("blur", showMsg());
to
$("#text").on("blur", showMsg);
This will also fix your $(this) problem. You can set that back now.

Can't execute simple JS by onclick from a button

This is almost embarassingly simple. I haven't touch JS for a while and now I can't do anything while clicking on a button...
The fiddle shows what I mean.
Click below
<div id='holder'></div>
<input type='button' value='below' onclick='onClick()'>
function onClick() {
alert('beep');
}
Smack me if it's something obvious. Which it most likely is...
You need to define the function before you assign it to onClick. See here, I have updated your fiddle.
Additionally I have commented you Jquery code, didn't think it was relevant.
<html>
<head>
<script>
function onClick() {
alert('beep');
}
</script>
</head>
<body>
<input type='button' value='below' onclick='onClick()'>
</body>
</html>

Javascript automatically click onclick

I am at a loss here, I am modifying a template for whmcs and I am trying to get an onclick event to launch when the page loads. I have tried the methods here but for the life of me I can't get it to work. How would you do this?
Here is the code:
<div><label><input type="radio" name="domainoption" value="incart" id="selincart" onclick="document.getElementById('register').style.display='none';document.getElementById('transfer').style.display='none';document.getElementById('owndomain').style.display='none';document.getElementById('subdomain').style.display='none';document.getElementById('incart').style.display=''" /> {$LANG.cartproductdomainuseincart}</label></div>
I got it to work guys. I appreciate all your help. Turns out, its a SMARTY template, so it was not parsing the {} correctly. Hence why it didn't work with any examples and 3 hours of me struggling for a stupid problem. I wasn't using {literal}.
Thanks for those that didn't put me down for asking a stupid question...
If you want to execute javascript on page load, you can use body onload
<script>
function load()
{
alert("Page is loaded");
}
</script>
<body onload="load()">
<h1>Hello World!</h1>
</body>
If you are okay with Jquery, Use trigger to run your click codes :
Example :
$(document).ready(function() {
$('#yourId').trigger("click");
});

focus() won't work - query

I have some input field, and I call this in my js file
$(document).ready(function () {$('#input_id').focus(); });
but it doesn't launch. Even, when I launch it in my chrome console, I get no focus. How can it be
This is working sample for a text input, just match with your page code and see what you are missing as compared to this.
I assume that you have referred jquery js already and any other jquery functions work well in your page.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function ()
{
$('#input_id').focus();
});
</script>
</head>
<body>
<input type="text" id="input_id"/>
</body>
</html>
In any case please make sure that there is no js error in your page as
$('#input_id').focus(); must work fine individually, so only thing looks wrong could be reference to jquery, else some js error before code reaches to .focus() call on input_id.
Also you can validate if on your page focus for the input working fine, for this keep $('#input_id').focus(); in a script tag just before your body page ends/closes,
to make sure input control, jquery reference are placed correctly and page has no js errors, if this way too focus doesn't work then something is wrong with any of these 3.

Categories