getting the id of an element in jquery onlclick? - javascript

this is a very simple example of a html page
<span class ="title" id="title_1">this is</span>
<span class="toggle" id="toggle_1>paragraph 1</span>
<span class ="title" id="title_2">this is</span>
<span class="toggle" id="toggle_2">paragraph 2</span>
im trying to do an onlcick event with the toggle functon, so basically when the user clicks title_1 the toggle_1 will hide and show, and if the user clicks title_2 it will show and hide toggle_2, i hope this makes sense.
(document).ready(function() {
$('.toggle').hide();
$('#title_').click(function() {
$('#toggle_').toggle('slow');
});
});
i started my jquery like this, just don't know what to do from here. thanks

You could:
$('.toggle').hide();
$('.title').click(function() {
$(this).next(".toggle").toggle('slow');
});

Try this
(document).ready(function() {
$('.toggle').hide();
$('.title').click(function() {
$(this).closest('.toggle').toggle('slow');
});
});

Related

HTML JS - Disable clicking possibility through the CSS class

,hello Gentlemann please
1° How can i disable the click Behavior onclick through the css class please?
2° I can't use ID in the HTML Element because i'm not able to insert any ID in the code, so i need to use the Class the make the magic happen.
3° In all my Tries, none of them still works.
Element and CSS Class that i need to disable if clicked:
<label class="inner all disabled reserved my_numbers">
<span class="p5_effect_tooltip_b top">
<span class="numero">999999<br><small>pending<br>payment</small></span>
<span class="p5_custom_tooltip_b">Pre-Reserved: Jim</span>
</span>
</label>
Class: label.inner.all.disabled.reserved.my_numbers
1° First Try:
jQuery(document).ready(function() {
$("#inner.all.disabled.reserved").click(function() {
return false;
});
});
2° Second Try:
$(document).ready(function() {
$(".label.inner.all.disabled.reserved").click(function() {
$("label").off("click");
});
});
3° Third Try:
$(document).ready(function() {
$("#inner.all.disabled.reserved").click(function() {
return false;
});
});
4° Try:
$('#inner.all.disabled.reserved.my_numbers').disabled = true
I find it best to use the Event. You can use .preventDefault() to stop the event action.
Example:
$(function() {
function preventClick(e) {
e.preventDefault();
console.log(e.target.nodeName + " Click Prevented");
return false;
}
$("label.inner.all.disabled.reserved").click(preventClick).children().click(preventClick);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="inner all disabled reserved my_numbers">
<span class="p5_effect_tooltip_b top">
<span class="numero">999999<br><small>pending<br>payment</small></span>
<span class="p5_custom_tooltip_b">Pre-Reserved: Jim</span>
</span>
</label>
I specially registered the console.log() so that you could see that the click can be done only once.
Was it necessary?
$(".inner.all.disabled.reserved").click(function(){
console.log('This message will not show!');
}).off('click');
.inner.all.disabled.reserved:hover {
color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="inner all disabled reserved my_numbers">
<span class="p5_effect_tooltip_b top">
<span class="numero">999999<br><small>pending<br>payment</small></span>
<span class="p5_custom_tooltip_b">Pre-Reserved: Jim</span>
</span>
</label>

Check if all divs are hidden after click event

I have divs with a link in each. After clicking on the link, the div is hidden. I want to make a redirection to another page when all the divs are hidden.
Here is my code:
<div class="checkhide">
<div>
<div>
<a href="#" class="btn" onClick="$(this).parent().parent().parent().hide()" role="button">
<span class="glyphicon glyphicon-remove">
</span>
</a>
</div>
</div>
</div>
Not clear what you mean 'all' div, it seems this onClick="$(this).parent().parent().parent().hide()" only works for ONE div.
Maybe if add class 'hidden' to <div> when click <a > , and if you know how many div there are, like 4.
Then check whether 'all' div class contain 'hidden', by:
$("a").on('click', function(){
// here should be some div you want to hide
$(".checkhide").addClass("hidden");
})
var hidden_div_number = $("div").find("[class*='hidden']").length;
if (hidden_div_number==4){
// do something;
}
I solved the problem. That was just a typo. I forgot the "." in my javascript:
$(function() { if ( $(".checkhide:visible").length === 0) alert('all are hidden'); });
Thank you all!

jQuery trigger excluding with .not()

So I want clicking on the img "tri" to hide the div "popu". But the img is a child of the div popu so i tried .not(). It didn't work. Note: Also I don't want div "textb" to trigger the hide.
HTML
<div id="re" class="column"><div id="s" class="popu"><img class="tri" src="img/whtri.png"/>
<div class="textb"><center style="font-size:14px;">Item Title</center>
<span style="font-size:12px;">Description this is an item that is very good and i like it very much! I like
<span class="highl">More...</span>
</span>
<span style="">
</span>
</div>
</div>
JQuery
$("body").click(function (e) {
if (!$(e.target).closest(".popu").length.not(".tri")) {
$("#s").hide(200)
}
});
Any Help Would Be Awesome!
You can use .parent():
$('img.tri').click(function() {
$(this).parent().hide();
});
or .closest():
$('img.tri').click(function() {
$(this).closest('.popu').hide();
});
Fiddle Demo
You can also use :
$('.tri').click(function(){
$('#s').hide();
});

recognize which span tag triggered javascript function in div

I have a div html element that has a click event set on it inline (not the way I want to do it but legacy code). See here
<div class="myDiv" onclick="triggerJavascript();" id="myDiv">
<span id="text1">Text 1<span>
<span id="text2">Text 2<span>
<span id="text3">Text 3<span>
<span id="text4">Text 4<span>
<span id="text5">Text 5<span>
What I want to do is recognize which span tag the click event originates from test5, then dont carry out the logic in triggerJavascript function, otherwise complete logic in triggerJavascript.
How can I set this up? I am working with jquery.
You can use event.target in order to access the element. However, in order to get to this element you have to change your onclick attribute a little bit:
<div class="myDiv" onclick="triggerJavascript(event);" id="myDiv">
then you can access event in triggerJavascript:
function triggerJavascript(e){
var element = e.target;
}
See also this answer for a more detailed explanation why event is needed.
Demo ; Demo with text5 check:
<script>function triggerJavascript(e){
if(e.target.id === "text5")
alert("text 5 hit");
e.stopPropagation();
}
</script>
<div class="myDiv" onclick="triggerJavascript(event);" id="myDiv">
<span id="text1">Text 1</span> <!-- closing tags -->
<span id="text2">Text 2</span>
<span id="text3">Text 3</span>
<span id="text4">Text 4</span>
<span id="text5">Text 5</span>
</div>
You can't use onclick="triggerJavascript();", or the event target (the span which was clicked) will not be passed to the event handler.
Since you state you're using jQuery, use this:
$('#myDiv').click(function(evt) {
alert("The target is: " + evt.target.id);
});
HTML
<div class="myDiv" onclick="triggerJavascript(event);" id="myDiv">
<span id="text1">Text 1<span>
<span id="text2">Text 2<span>
<span id="text3">Text 3<span>
<span id="text4">Text 4<span>
<span id="text5">Text 5<span>
</div>
JavaScript
<script type="text/javascript">
function triggerJavascript(event) {
// event.target will catch the clicked element
if (event.target.id !== 'text5') {
// do something
}
}
</script>
DEMO
If you can't change the HTML, you could try something like this:
var triggerJavascript = (function(){
var clicked;
$("#myDiv span").click(function(e){
clicked = $(e.target).attr("id");
});
return function() {
if (clicked == "text5") {
return;
}
//do something cool...
}
})();
Although I guess, there's no guarantee that the jQuery click handler is executed before the actual triggerJavascript logic, so this might not always work correctly.

Preventing the user to click a div when its fading out

I'm trying to create a filtering system where the user can filter for answers using more than one category, but the user can't choose more than one option from each category, so upon clicking an option from any category, that category will fade out ... I have the following little problem, if the user clicks on another option from the same category while it's fading out, he will be able to choose two options from the same category ... How can I disable any click on the category when it's fading out ??
Here is the code I'm using:
HTML:
<div class="search-list" id="program-list">
<span class="search-title">Category 1</span>
<span data="program-list">Option 1</span>
<span data="program-list">Option 2</span>
<span data="program-list">Option 3</span>
<span data="program-list">Option 4</span>
<span data="program-list">Option 5</span>
<span data="program-list">Option 6</span>
</div> <!-- search-list -->
<div class="search-list" id="trainer-list">
<span class="search-title">Category 2</span>
<span data="trainer-list">Option 1</span>
<span data="trainer-list">Option 2</span>
<span data="trainer-list">Option 3</span>
<span data="trainer-list">Option 4</span>
</div> <!-- search-list -->
<div class="search-list" id="issue-date">
<span class="search-title">Category 3</span>
<span data="issue-date">Option 1</span>
<span data="issue-date">Option 2</span>
<span data="issue-date">Option 3</span>
<span data="issue-date">Option 4</span>
<span data="issue-date">Option 5</span>
<span data="issue-date">Option 6</span>
</div> <!-- search-list -->
JQuery:
$('.search-option').click(function(){
var x = $(this).html();
$('#filtered-items').append(x);
$(this).parent('.search-list').fadeTo('slow', 0);
$('#filtered-items > span').click(function(){
$('#'+$(this).attr('data')).fadeTo('slow', 100);
$(this).hide();
});
});
I tried using the following JQuery code but it didn't work:
$('.search-option').click(function(e) {
e.preventDefault();
});
Thanks in advance...
You can set an indicator in your function:
$('.search-option').click(function(){
if(indicator) {
return false
}
...
}
The indicator is something set when the fade starts and clears when it ends. It could be:
a javascript variable,
or - you can check the current opacity,
or - or you can set/clear a class name during the fade.
Simply remove the 'search-option' class from all options in a category once it is clicked.
You can set the click event to null once it has fired:
$('.search-option').click(function(){
$(this).click(null); // set click to do nothing on the clicked element
var x = $(this).html();
$('#filtered-items').append(x);
$(this).parent('.search-list').fadeTo('slow', 0);
$('#filtered-items > span').click(function(){
$('#'+$(this).attr('data')).fadeTo('slow', 100);
$(this).hide();
});
});
This will only turn off the click action on the specifically clicked element so your other filters will continue to work until each of them has been selected once. If you want to re-enable the click function after everything has happened you can save the function, disable it, then re-enable at the end as below:
$('.search-option').click(function(){
var fun = $(this).click;
$(this).click(null); // set click to do nothing on the clicked element
// OTHER CODE
$(this).click(fun);
});
Since this all hinges on the user clicking on an element with the class "search-option", just remove that class from the element and its siblings before starting the fade and then restore it with a callback after the fade runs. That way, nobody can click twice:
$('.search-option').click(function() {
var x = $(this).html();
$('#filtered-items').append(x);
$(this).parent().children().removeClass('search-option');
$(this).parent('.search-list').fadeTo('slow', 0, function() {
$(this).children().addClass('search-option');
});
});
Hey everyone ... I finally solved it, I had to change the whole structure of the function and use the methods "unbind" and "bind" ... Here is the solution if anyone is interested in it:
$('.search-option').click(work);
});
function work(){
var x = $(this).html();
$('#filtered-items').append(x);
$(this).unbind('click');
$(this).siblings('a').unbind('click');
$(this).parent('.search-list').fadeOut('slow');
$('#filtered-items > span').click(function(){
$('#'+$(this).attr('data')).fadeIn('slow');
$('#'+$(this).attr('data')).children('a').bind('click',work );
$(this).remove();
});
};
Now the user can't choose more than one option at each category ....
Try:
$('.search-option').click(function(){
var x = $(this).html();
$('#filtered-items').append(x);
// ADDED LINES
$(this).parent('.search-list').find('a').each(function(){
$(this).click(function(){return false;});
});
// END: ADDED LINES
$(this).parent('.search-list').fadeTo('slow', 0);
$('#filtered-items > span').click(function(){
$('#'+$(this).attr('data')).fadeTo('slow', 100);
$(this).hide();
});
});

Categories