Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  68] [ 2]  / answers: 1 / hits: 20313  / 13 Years ago, tue, february 21, 2012, 12:00:00

I have a link. When some one clicks on that I want to check some conditions before letting it work. If it's false the default action should be prevented.



$(.pager-next a.active).click(function(event) {
if (!a == 1) {
event.preventDefault();
}
});


The link should only work if a is equal to 1. Is the above code correct. a is set to 1 if a particular condition is met. The link should only work if the condition is met.


More From » jquery

 Answers
9

Assuming by 'should only work if a is equal to 1' you mean the text of the a element is equal to 1, try this:



$(.pager-next a.active).click(function(event) {
if ($(this).text() != 1) {
event.preventDefault();
}
});


You can amend text() to use whichever attribute of the element is available to you in jQuery.



UPDATE




my a is a var which hold the value 0 until a condition is met.




In which case, the problem was simply that your equality operator was incorrect:



$(.pager-next a.active).click(function(event) {
if (a != 1) {
event.preventDefault();
}
});

[#87318] Sunday, February 19, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yvettel

Total Points: 517
Total Questions: 101
Total Answers: 102

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
yvettel questions
Tue, Jul 27, 21, 00:00, 3 Years ago
Thu, Aug 13, 20, 00:00, 4 Years ago
Wed, Apr 15, 20, 00:00, 4 Years ago
Wed, Apr 1, 20, 00:00, 4 Years ago
;