Javascript define a function within event handler [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
How can I write a function within the onclick event of a button
What I want to do is something similar to this:
<button onclick="(function(){
console.log('some code here')
})">My button</button>

You'll need to invoke the function:
<button onclick="(function(){
console.log('click')
})()">My button</button>

<button>
onclick="(function(){console.log('click')})()"
>Button
</button>

Related

onClick event executes as soon as I change the function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
i am trying to change the onclick value of a Button via Javascript. But in the moment I change its value, the function is executed directly.
document.getElementById('Send').disabled = false;
document.getElementById('Send').addEventListener("click", window.location.href = 'http://www.google.com');
Second parameter must be a function! Try:
document.getElementById('Send')
.addEventListener("click",
function(){window.location.href = 'http://www.google.com'});

No result when use "button" and innerHTML in HTML [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I make a button that once click will show "Hello World", But it does not work. I don't know where I made mistakes.
This is button an div.
<button type="button" onclick="myFunction():"> Click me to see the result</button>
<div id="demo"></div>
This is my script.
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
The colon should be removed from "myFunction()".

How to click a link using jquery or javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a link:
Manage Lists
I want to "click" this link using jquery or javascript. I've tried:
$("#panel2-2").click(function(){});
$("#panel2-2").trigger('click');
It doesn't work! How can I do a click like this using javascript?
You need to use the jQuery attribute equals selector.
Description: Selects elements that have the specified attribute with a value exactly equal to a certain value.
For your case, you would use something like this:
$( "a[href='#panel2-2']" ).click();
You need
$('[href="#panel2-2"]').trigger('click');
or
$('[href="#panel2-2"]').click();

Changing the body with jQuery when resize the window [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to change the body content when I resize the windows, but after 2 hours searching and trying, I get nothing.
This is my last jQuery code:
$(window).resize(function(){
if($(window).width<=320){
changeHTML();
}
});
var changeHTML=function(){
alert("wow");
//$('body').load('../index2.html');
}
The "index2.html" is something like this:
<div>
A lot of stuff
</div>
$(window).width is incorrect, as width() is a method. Use $(window).width() instead.
See working jsFiddle demo

How to pass javascript variable in class? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am writing a code for quiz,whenever one option is selected by the user from out of four option,i want to alert. I have something similar to my code here:-
<label class="alert optiona">
</lablel>
I am having value
data.correct="optiona";
I need to send pass the value in javascript.When i am passing following syntax , it doesnt send the value of data.correct.
$(".alert "+data.correct).attr("class", "alert alert-"+ data.mode);
You're missing the dot in the selector and you have a space which means that the second class applies to a descendant of the .alert element.
Change
$(".alert "+data.correct)
to
$(".alert."+data.correct)

Categories

Resources