jQuery check if it is clicked or not
$( 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.
Solution 1:
You could use .data()
:
$("#element").click(function(){
$(this).data('clicked', true);
});
and then check it with:
if($('#element').data('clicked')) {
alert('yes');
}
To get a better answer you need to provide more information.
Update:
Based on your comment, I understand you want something like:
$("#element").click(function(){
var $this = $(this);
if($this.data('clicked')) {
func(some, other, parameters);
}
else {
$this.data('clicked', true);
func(some, parameter);
}
});
Solution 2:
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)
Solution 3:
<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>
Solution 4:
<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>