$( element ).click( function() {
});
How can I check if the element is clicked or not? I'm doing like that
function element() {
$( "#element" ).click( function() {
return 0;
} );
}
if( element() == 0 ) {
alert( "yes" );
} else {
alert( "no" );
}
But it's not returning anything.
This question is related to
jquery
Alright, before I go into the solution, lets be on the same line about this one fact: Javascript is Event Based. So you'll usually have to setup callbacks to be able to do procedures.
Based on your comment I assumed you have a trigger that will do the logic that launched the function depending if the element is clicked; for sake of demonstration I made it a "submit button"; but this can be a timer or something else.
var the_action = function(type) {
switch(type) {
case 'a':
console.log('Case A');
break;
case 'b':
console.log('Case B');
break;
}
};
$('.clickme').click(function() {
console.log('Clicked');
$(this).data('clicked', true);
});
$('.submit').click(function() {
// All your logic can go here if you want.
if($('.clickme').data('clicked') == true) {
the_action('a');
} else {
the_action('b');
}
});
Live Example: http://jsfiddle.net/kuroir/6MCVJ/
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
var val;
$(document).ready(function () {
$("#click").click(function () {
val = 1;
get();
});
});
function get(){
if (val == 1){
alert(val);
}
}
</script>
<table>
<tr><td id='click'>ravi</td></tr>
</table>
This is the one that i've tried & it works pretty well for me
$('.mybutton').on('click', function() {
if (!$(this).data('clicked')) {
//do your stuff here if the button is not clicked
$(this).data('clicked', true);
} else {
//do your stuff here if the button is clicked
$(this).data('clicked', false);
}
});
for more reference check this link JQuery toggle click
Using jQuery, I would suggest a shorter solution.
var elementClicked;
$("element").click(function(){
elementClicked = true;
});
if( elementClicked != true ) {
alert("element not clicked");
}else{
alert("element clicked");
}
("element" here is to be replaced with the actual name tag)
<script>
var listh = document.getElementById( 'list-home-list' );
var hb = document.getElementsByTagName('hb');
$("#list-home-list").click(function(){
$(this).style.color = '#2C2E33';
hb.style.color = 'white';
});
</script>
Source: Stackoverflow.com