Disable mouse in tag "li" [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm in doubt how to, disable the mouse over an image that is in such a tag "li".
I managed to disable the click event, but is necessary an event that even the mouse would go away and there would any action on the tag.
Like a java disable.
Only in jquery or javascript.
Tank's.
Disabling click:
$("#btnEmpresarial").removeAttr('onclick');

If you are looking to hide the mouse cursor, you can always use the CSS property cursor in this way:
cursor: 'none';
Living demo: http://jsfiddle.net/FhCLP/
If you also want to disable any click, you could use jQuery in this way:
$('#element').click(e){
e.preventDefault();
});
Living demo: http://jsfiddle.net/FhCLP/1/
Also, as pointed out by #KyleMit, you can even add the property cursor:not-allowed; if you want to show another cursor to specify the not-allowed state of the cursor.
Living demo: http://jsfiddle.net/FhCLP/2/

Use unbind method:
Unbind:
Removes handlers attached to the elements:
$( "#img li").unbind( "hover" );

If you've attached events to the item, e.preventDefault won't work because you're not stopping the default action. I think what you want to do is call .unbind(). Let's say you attached event handlers all the li elements in the following HTML but you didn't want to fire any events on the second item.
<ul >
<li>Enabled</li>
<li class="prevent">Disabled</li>
</ul>
Using jQuery, you can remove any bindings that have been applied to any items with the prevent class
$('.prevent').unbind();
If it improves clarity, you can style your element with the following CSS
.prevent {
cursor:not-allowed;
}
jsFiddle

Related

jQuery loses added class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have several buttons with a class mybtn. I need to set a class mybtn-active on the one that is clicked, and remove it from the one that has it at the moment.
$('.mybtn').on('click', function () {
$('.mybtn-active').removeClass('mybtn-active');
$(this).addClass('mybtn-active');
});
When I click on a button, currently active loses its class, as it should. But the clicked one doesn't get the class.
I passed through the script with Chrome debugger and it works. It just loses the class when the code exits the script. Any ideas?
[SOLUTION] by #musefan
The buttons were <a> tags in the background, which I didn't think about because this is the code I inherited, I didn't write the HTML. And that was the problem. See the accepted answer.
Assumptions
I am going to take a massive gamble here, but it's the only thing I can think of that would explain what the OP is describing and could actually happen with that code. We would need the OP to provide HTML to validate my assumption.
I am also assuming that the class selector in the sample code is a typo of the question, and is not a problem in the original code (as the OP specifically says the removal of the classes is working correctly).
My assumptions have since been validated by the OP in comments on this answer and other answers.
Problem
I expect your problem is that you are using either a elements and your click is actually reloading the page, or using buttons that are also having the same effect, thus causing all classes to revert to default.
Solution
You can fix this by using the arguments of the click event as follows:
$('.mybtn').on('click', function (e) {
e.preventDefault(); // This prevent the hyperlink from reloading the page.
$('.mybtn-active').removeClass('mybtn-active');
$(this).addClass('mybtn-active');
});
You are missing a
.
in the selector on your second line.
It should be
$('.mybtn-active').removeClass('mybtn-active');
As I have mentioned in the comment, it is typo mistake. You have to change $('mybtn-active') to $('.mybtn-active'). Look at the snippet.
$('.mybtn').on('click', function () {
$('.mybtn-active').removeClass('mybtn-active');
$(this).addClass('mybtn-active');
});
.mybtn-active {
font-size:25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="mybtn">Button1</button>
<button class="mybtn">Button2</button>
<button class="mybtn">Button3</button>
<button class="mybtn">Button4</button>

onClick add input field [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to add a transparent input text field in the centre of DIV #mainC each time the button is clicked.
JsFiddle
I tried doing this with jquery but did not succeed.
use append():
$("#abc").click(function() {
$("#main").append('<input type="text" />');
});
demo: http://jsfiddle.net/6nkd5/5/
Well there's multiple things wrong with your code:
You use the same ID multiple times (ID's can only be used once, classes can be used multiple times).
Calling a function from a external JavaScript source will result in the function not running.
You're trying to use jQuery when jQuery is not loaded
$('<input type="aText" id="aText">').appendTo('#main') is not a valid jQuery, instead try to use: $('#main').append('<input type="text" id="aText">');
aText is not a valid input type attribute value
There is no library selected at the upper left corner in jsfiddle. Try to select jquery there.
Use append() and try not to specify onclick inline. You can register the event with jQuery when document is ready. Registering methods this way is preferred because you separate logic from your HTML (Check unobtrusive javascript).
This is how you would do it preferably:
$('#abc').on('click', function(){
$('#mainC').append('<input type="aText" id="aText">')
})
Check also jsfiddle
The problem you had with your jsFiddle was also that no jQuery script was selected.

make active while click using javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some content which I have displayed by id when click. I need a javascript function for active links (class name is .active), and it will remove(.active) when other links have been active,
An example of the code structure is:
<ul class="product">
<li>Myanmar</li>
<li>Madagascar</li>
<li>United States of America</li>
<li>Ethiopia</li>
<li>Brazil</li>
<li>Australia</li>
<li>China</li>
<li>Kenya</li>
<li>Canada</li>
</ul>
but to add to Davids answer this is what you are going to want to do:
first check if the link you are clicking has the class of active before you do anything so you can avoid flicker (I am assuming you are going to jump to divs either animated or not)
$('ul.product a').click(function(){
if(!$(this).hasClass("active")){
$(".active").removeClass("active");
$(this).addClass("active");
}else{
return false;//this prevents flicker
}
});
Try something like this:
$('ul.product a').click(function(){
$('ul.product a').removeClass("active");
$(this).addClass("active");
});
Fiddle: http://jsfiddle.net/s8nky/
check : http://jsfiddle.net/en2LT/
try this...when clicking the link you should addclass active and removeclass active

How can add span to parent menu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how can I add span that have arrow picture for the ul.menu li that have child elements (ul.sub-menu li ).
HTML
<ul class="menu">
<li>hhh</li>
<li>hhh
<ul class="submenu">
<li>kkk</li>
<li>kkk</li>
<li>kkk</li>
</ul>
</li>
<li>hhh</li>
</ul>
how can do that by jquery or css
This jQuery code should work:
$(".submenu").parent().append("<span class='arrow'></span>");
There is no proper way exist to give you perfect answer, because you don't provide complete code (JS, HTML).
But as your requirement: if you want to add a span to parent menu -
First you need to select parent element, for this you can use .parent() jquery selector.
Jquery selector .parent() : - Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
Here complete documentation of jquery .Parent()
And second:
use .append() - for adding element to the end of selected element
$(".submenu").parent().append("<span class='arrow'></span>");
use .prepend() - for adding element to the beginning of selected element
$(".submenu").parent().prepend("<span class='arrow'></span>");
Try this,
$("ul.submenu").closest('li').children('a').append("<span> ▼</span>");
Demo
Use this if you want add class to ul tag along with menu
$(".submenu").parent().parent().append("<span class='arrow'></span>");

Collapsible FAQ with links that jump to and expand other questions

I currently have this code for an FAQ list of questions and answers. The headings, questions and answers are expandable/collapsible on click.
http://jsfiddle.net/7RbCZ/
However, one feature I need is to be able to, within answer text, link to other questions so that the page jumps to and opens up this question.
Currently I have written (line 6 HTML):
(See Question 3)
in the answer to Question 1, and given Question 3 an id (line 18 HTML):
<li class="list-level-2" id="question-3">Question 3?
This is not working but hopefully demonstrates what I want to achieve.
Thanks very much for any help.
Edit: Thanks to marbor3 below. Re:their solution, does any one have any ideas about how to get the page to jump to this question/answer? Also have a problem that if the question linked to is already open then the trigger(click) makes it invisible.
You can catch click event on the link and trigger click event in element which id is in the href
$(".triggerNextQuestion").click(function(event) {
event.preventDefault();
var question = $($(this).attr('href'));
if(!question.parent().is(':visible')) {
question.parent().trigger('click');
}
question.trigger('click');
});
You will need to check if parent is visible also, to show it if it's not.
It's a starting point, sure needs some modifications, like showing question in after parent is shown.
Here's fiddle

Categories