hide div A if div B is show - javascript

i writing simple script to popup quick info using jquery. When user click view, some info will show using toggle() and hide when user click again. And this script will loop 10 times.
But the problem is i want this popup only show one time and the rest will hide, now when user click view 1 and view 2 all popup will show at same time.
You can check my jsFiddle click here
<script>
$(document).ready(function() {
$("#view_1").click(function(e) {
e.stopPropagation();
$(".box_1").toggle();
});
$(document).click(function() {
var $el = $(".box_1");
if ($el.is(":visible")) {
$el.fadeOut(200);
}
});
});
</script>
*im not sure how to combine this script in one function

Here is your working demo
$("a").click(function() {
$('.contact_box').hide();
$(this).next('div').show();
});

use hide() to hide your corresponding box..
$("#view_1").click(function(e) {
e.stopPropagation();
$(".box_2").hide(); <-----here
$(".box_1").toggle();
});
$("#view_2").click(function(e) {
e.stopPropagation();
$(".box_1").hide();<-----here
$(".box_2").toggle();
});
fiddle here

should be :
before $(".box_1").toggle(); this hide $(".box_2").hide(); and before $(".box_2").toggle(); this hide $(".box_1").hide();
This will works.

Hi Use the code below,
<script>
$(document).ready(function() {
$("#view_1").click(function(e) {
e.stopPropagation();
$(".box_1").toggle();
var $el = $(".box_2");
if ($el.is(":visible")) {
$el.fadeOut(200);
}
});
$(document).click(function() {
var $el = $(".box_1");
if ($el.is(":visible")) {
$el.fadeOut(200);
}
});
});
</script>
Hope this solves your problem

Toggle also has callback function you can use it.
$(".box_1").toggle('slow', function() {
// show hide code or fadeIn fadeOut or other animation
$(".box_2").fadeOut('fast');
});

Try this:
<div style="position:relative">
<a style="cursor: pointer" id="view_1" class="my_view">View 1</a>
<div class="contact_box box_1 content_box" style="display: none;">
<div class="left" style="width:150px; margin-right:10px;">
<img src="http://www.dunking-devils.com/images/samsung-logo_100x100.jpg"
style="max-width:150px" />
</div>
<div class="contact_info left" style="width:250px">
<div class="company_name">DIV A</div>
<table width="100%" border="0" class="table_contact">
<tr>
<td width="29">Name</td>
<td>: Jenson Button</td>
</tr>
<tr>
<td style="padding-right:0px">Phone</td>
<td>: 012-66558741</td>
</tr>
<tr>
<td style="padding-right:0px">Email</td>
<td>: jb#gmail.com</td>
</tr>
</table>
</div>
</div>
</div>
<br>
<br>
<div style="position:relative">
<a style="cursor: pointer" id="view_2" class="my_view">View 2</a>
<div class="contact_box box_2 content_box" style="display: none;">
<div class="left" style="width:150px; margin-right:10px;">
<img src="http://www.dunking-devils.com/images/samsung-logo_100x100.jpg"
style="max-width:150px" />
</div>
<div class="contact_info left" style="width:250px">
<div class="company_name">DIV B</div>
<table width="100%" border="0" class="table_contact">
<tr>
<td width="29">Name</td>
<td>: Jenson</td>
</tr>
<tr>
<td style="padding-right:0px">Phone</td>
<td>: 012-88542215</td>
</tr>
<tr>
<td style="padding-right:0px">Email</td>
<td>: ac#gmail.com</td>
</tr>
</table>
</div>
</div>
</div>
$(document).ready(function() {
$('.my_view').click(function(e) {
$('.my_view').siblings('div').each(function(){$(this).hide()});
$(this).siblings('div').toggle();
e.stopPropagation();
});
$(document).click(function() {
$('.my_view').siblings('div').fadeOut(200);
});
});

Related

javascript search function for hiding everything not matching input

I would like to implement a search function which uses input to find text on my page without pressing enter and hiding everything else not matching the current input.
Tricky stuff: the text on my page is hidden by default and only visible by hovering its container tiles. Also the entire page is built by scripts. This is the structure:
<div id="data" class="grid-container">
<div class="grid-item">
<div class="inside" id="item0">
<h2 id="contents0" class="content-title" href="some link">some headline</h2>
<div class="collapsing">
<br>
<table>
<tbody>
<tr id="tr0">
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
<tr>
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
<tr>
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
so there are various grid-item's like this one with different amounts of tabledata's.
this is the input:
<input class="search" type="search" placeholder="Search" id="input">
Any ideas on how to do this for my special case?
I want to hide the entire grid-item if nothing inside matches the current input and I want to hide only the tabledatas not matching the current input if there are matches with one ore more other tabledatas in the same grid-item.
this is my attempt at editing the suggestion down there:
<script>
function findDom(val) {
if (val === '') {
document.querySelectorAll('.hideElem').forEach(function(item) {
item.classList.remove('hideElem')
})
return;
}
document.querySelectorAll('.content').forEach(function(item) {
if (item.textContent.trim().includes(val)) {
item.classList.remove('hideElem')
}
else {
item.classList.add('hideElem')
}
})
}
document.getElementById('input').addEventListener('keyup', (e) => {
setTimeout(function() {
findDom(e.target.value.trim())
}, 2000)
})
</script>
CSS:
.hideElem {
display: none;
}
However it does not work at all...
Please check the inline comments for description
// this function will first check if the search value is empty , then
// it will get all the a tag that are visible and will hide them
function findDom(val) {
if (val === '') {
document.querySelectorAll('.showElem').forEach(function(item) {
item.classList.remove('showElem')
})
return;
}
// otherwise it is going to get the content from each a tag and will check
// if the conent includes the search keyword, in that case it will show
// the a
document.querySelectorAll('.content').forEach(function(item) {
// hiding previously displayed a tags
item.classList.remove('showElem')
// check if current a tag contains this text
if (item.textContent.trim().includes(val)) {
item.classList.add('showElem')
}
})
}
// this will get the value entered in the text field
document.getElementById('ip').addEventListener('keyup', (e) => {
// wait for 2 secs for and search for the value, without timeout it will
// search for every text entered
setTimeout(function() {
findDom(e.target.value.trim())
}, 2000)
})
.content {
display: none;
}
.showElem {
display: block;
}
<div id="data" class="grid-container">
<div class="grid-item">
<div class="inside" id="item0">
<h2 id="contents0" class="content-title" href="some link">some headline</h2>
<div class="collapsing">
<br>
<table id='table'>
<tbody>
<tr id="tr0">
<td class="tabledata">
<a class="content" href="some link">some 1</a>
</td>
<td class="tabledata">
<a class="content" href="some link">some 2</a>
</td>
<td class="tabledata">
<a class="content" href="some link">some 3</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<input class="search" id='ip' type="text" placeholder="Search" id="input">

Need to toggle a div for each href using JQuery

Can someone help me with this. I need to show course description hidden inside a div when user clicks on the course link and toggle after any link is clicked
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CourseID)
</td>
<td>
<a title="Click to See Course Description" class="info" id="#Html.DisplayFor(modelItem => item.Title)" href="#">#Html.DisplayFor(modelItem => item.Title)</a>
</td>
}
<div class="myshow" id="Calculus_show" style="display:none">
<p>
Information about a Course
</p>
</div>
and I have my js code
$(document).ready(function () {
$('.info').on('click',function (e) {
e.preventDefault();
id = '#' + $(this).attr('id');
id = id + '_show';
$('div.myshow').attr('id', id);
$(id).toggle();
})
});
Here div.myshow does not show on toggle.
You can try something like this:
First find all div that are visible, hide them. Then using this, navigate to necessary div and show it.
$(".action").on("click", function(){
var self = this;
$(".info:visible").fadeOut(function(){
$(self).parent().find(".info").fadeIn();
});
});
.tile, .info{
font-weight:bold;
padding:5px;
margin:5px;
border:gray;
}
.info{
color:#333;
background:#ddd;
display:none;
}
.visible{
display:block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="tile">
<a class="action" onclick="show(1)">Show 1</a>
<div class="info visible">Information of 1</div>
</div>
<div class="tile">
<a class="action" onclick="show(2)">Show 2</a>
<div class="info">Information of 2</div>
</div>
<div class="tile">
<a class="action" onclick="show(3)">Show 3</a>
<div class="info">Information of 3</div>
</div>
<div class="tile">
<a class="action" onclick="show(4)">Show 4</a>
<div class="info">Information of 4</div>
</div>
$(document).ready(function () {
$('.info').on('click',function (e) {
e.preventDefault();
id = '#' + $(this).attr('id');
id = id + '_show';
$('div.myshow').attr('id', id);
$(id).toggle();
})
});
Just replace above code with below code, problem is that you are adding # in the id attribute itself instead of selector.
$(document).ready(function () {
$('.info').on('click',function (e) {
e.preventDefault();
id = $(this).attr('id');
id = id + '_show';
$('div.myshow').attr('id', id);
$('#' + id).toggle();
})
});
Hope this helps.
It looks like on the "$('div.myshow')" line you are changing the ID of the div to have a # in it for no reason, which is causing the script to break. I simply commented it out and it gave me the results you are wanting. A hide to the myshow div was also added so when a course is selected, then another course is selected, it hides the previous courses div and opens the new one's. I assumed that everything being generated in the HTML is coming out correctly when I made this.
Modified the code to show how it would work with multiple links.
$(document).ready(function () {
$('.info').on('click',function (e) {
e.preventDefault();
id = '#' + $(this).attr('id');
id = id + '_show';
//$('div.myshow').attr('id', id);
$('.myshow').hide();
$(id).toggle();
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
1234
</td>
<td>
<a title="Click to See Course Description" class="info" id="Calculus" href="#">Calculus</a>
</td>
</tr>
<tr>
<td>
1234
</td>
<td>
<a title="Click to See Course Description" class="info" id="Programming" href="#">Programming</a>
</td>
</tr>
</table>
<div class="myshow" id="Calculus_show" style="display:none">
<p>
Information about Calculus
</p>
</div>
<div class="myshow" id="Programming_show" style="display:none">
<p>
Information about Programming
</p>
</div>

how to hide a parent div if image is missing

I'm trying to use a script to change the visibility of a div if an image inside it is missing
here is the div:
<div id="bike01">
<a href="stock/bike1.html">
<table width="900" border="0" cellpadding="3" cellspacing="0" class="table">
<tr>
<td width="245" rowspan="2"><img id="bike1" src="stock/bike1/images/image1.jpg" name="bike1" width="245" height="184" />
</td>
<td width="468"><div id="title1"></div></td>
</tr>
<tr>
<td colspan="2"><div id="desc1"></div></td>
</tr>
</table>
</a>
</div>
and here is my script (I am a complete beginner when it comes to scripting - ive just picked this up from researching my question)
<script>
$("#bike1").error(function () {
$("#bike01").css({visibility:"hidden"});
});
</script>
Basically if the img#bike1 is missing i want the whole div #bike01 to disappear.
Why not do:
<img id="bike1" src="stock/bike1/images/image1.jpg" onerror="this.style.display='none';" />
How do I hide broken images in javascript?
<img id="bike1" src="stock/bike1/images/image1.jpg" onerror='$("#bike01").css({visibility:"hidden"});' />
Try this:
<script>
$("#bike1").on('error', function() {
$("#bike01").hide();
});
</script>

Show/Hide values in table

I have a table structured like that:
<table id="example">
<thead>
<tr>
<th>Header1</th>
<th>Header2</th>
<th>Header3</th>
</tr>
</thead>
<tbody>
<tr>
<td>some php to get custom field</td>
<td>also custom field- the loop is above so this is working as it should</td>
<td>
<a href="#" class="showhide">
<div class="more">
.. the content I want to show when user clicks a class="showhide"
</div>
</a>
</td>
<tr>
</tbody>
</table>
I tried to implement with jQuery, but nothing happens. This is the jQuery I tried:
$(function () {
$('.showhide').click(function () {
$(this).next().toggle()
});
});
I also make it with document.ready and still not working. Is there any other way to do that?
All I want is that the content of the div class="more" is to be shown under the row in the table (and in front of the next one), of course when the a class="showhide" is clicked.
I am using this together with plugin DataTables, but this is not causing problems.
The class you have is showmore not showhide
Live Demo
$(function() {
$('.showmore').click(function(){
$(this).next().toggle()
});
});
Edit based on OP comments: I have shorten the html to make it simple and took div out of a tag.
Live Demo
Html
<table id="example">
<tbody>
<tr>
<td> Show / Hide
<div class="more">.. the content I want to show when user clicks a class="showmore"</div>
</td>
</tr>
</tbody>
</table>
Javascript
$(function () {
$('.showhide').click(function () {
$(this).next().toggle()
});
});
Either change this in the markup:
class="showmore">
to this:
class="showhide">
or do this:
$('.showmore').click(function (e) {
e.preventDefault();
$(this).next().toggle()
});
Note:
This is invalid html:
<a href="#" class="showhide">
<div class="more">
.. the content I want to show when user clicks a class="showhide"
</div>
change to this:
show more
<div class="more">
.. the content I want to show when user clicks a class="showhide"
</div>
or you can try this one with .children() not with .next():
$('.showmore').click(function (e) {
e.preventDefault();
$(this).children('more').toggle();
});
TRYOUT IN FIDDLE HERE
$(document).on('click','.showmore',function(){
//your code..
})

jQuery click to show/hide not functioning properly

I'm currently creating a page at this link. I'm using this javascript in my header (below my CSS):
<script type="text/javascript" src="http://static.tumblr.com/txc9jrr/yNxmd3lq6/jquery.js">
</script>
<script type="text/javascript">
$('.toggle').click(function(){
var $tr = $(this).closest('tr');
$tr.add($tr.next('.edit')).toggle();
});
$('.cancel').click(function(){
var $tr = $(this).closest('tr');
$tr.add($tr.prev('.view')).toggle();
});​
</script>
and this HTML:
<table>
<tr class="view">
<td>
<a class="toggle" class="icon-button" href="#">Edit</a>
</td>
</tr>
<tr class="edit" style="display:none">
<td>
hi
<a class="cancel" href="#">Cancel</a>
</td>
</tr>
</table>
In theory, it should work... However when I implement it into my page, it ceases to function. Does anyone have a clue as to what my problem is?
change
<a class="toggle" class="icon-button" href="#">Edit</a>
// ^--------------^---------------------Cannot define two classes like this
to
<a class="toggle icon-button" href="#">Edit</a>
// ^--------------------------------Try like this
DEMO
I think you should try this
$('.toggle').click(function(){
if ($(".edit").css('display') == 'none') {
$(".edit").css('display', 'block');
}
}
i think this will help you

Categories

Resources