jQuery find id then do action - javascript

I am trying to make this little code work. I have a normal drop-down menu and I want to turn it into a drop-down list (select).
The normal menu has a "tm_active" id attached to show which page you are on. I want to use jquery and find this id, then when page loads to select the right item according to what page you are on (not the first item in the list).
What am I doing wrong guys?
Thank you.
$('table.topMenu tr').each(function(){
var list=$(this),
select=$(document.createElement('select')).insertBefore($(this).hide()).change(function(){
window.location.href=$(this).val();
});
$('>td a', this).slice(2).each(function(){
var option=$(document.createElement('option'))
.appendTo(select)
.val(this.href)
.html($(this).html());
if($(this).attr('id') === 'tm_active'){
option.attr('selected','selected');
}
});
list.remove();
});
HTML
<table>
<tr>
<td class="top">
Section 1
<div class="sub">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</td>
<td class="top" id="tm_active">
Section 2
<div class="sub">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</td>
<td class="top">
Section 3
<div class="sub">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</td>
</tr>
</table>

You are checking the id of the anchor tag you should check the parent td:
$(this).parent().attr('id') === 'tm_active'
FIDDLE

Related

Make parent link unclickable in submenu toggle

I am trying to make a sub-menu toggle on mobile but it keeps on redirecting to the parent menu link and adding event.preventDefault(); making entire sub-menu links unclickable. Any idea how to make only first .has-childern a link unclickable?
Markup
<div class="menu-container">
<ul class="nav-menu">
<li class="menu item"> Home </li>
<li class="menu-item has-childern"> About
<ul class="submenu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</li>
<li class="menu-item has-childern"> Services
<ul class="submenu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</li>
<li class="menu item"> Contact </li>
</ul>
</div>
jQuery
jQuery(document).ready(function(){
jQuery('.menu-container .submenu').hide();
jQuery('.menu-container .has-childern a').click(function() {
event.preventDefault();
jQuery(this).parent().children('.submenu').toggle();
});
});
JsFiddle https://jsfiddle.net/p5fLsvcq/
You code affect all nested children instead just direct child
use '>' for select just a direct first level child in selector:
jQuery('.menu-container .has-childern > a').click(/*...*/)

JQUERY - toggle divs based on ID

I have a large number of DIVs on a page. Each being a container for an Unordered List. Above each DIV is a header text which consists of an element with an anchor.
e.g.
<h2><a id="header1" href="#" > Header 1 </a></h3>
<div id="container1">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<h2><a id="header2" href="#" > Header 2 </a></h3>
<div <div id="container2">>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
I need to have all these DIVs hidden until the header (anchor) is clicked. If the user clicks header it should toggle show/hide DIV
How can I accomplish this in JQUERY in a way that I can have one onClick function for all the DIVS, possibly using an id to differentiate between divs?
<h2> Header 1 </h3>
function toggleDiv(id) {
}
but in JQUERY ?
SOLVED!!!!
<script>
$(function(){
$('.toggle-link').on('click', function() {
var linkId = $(this).attr('data-div-id');
$('#' + linkId).toggle();
});
});
</script>
<h2> Header 1 </h2>
<div id="div1">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<h2> Header 2 </h2>
<div id="div2">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
You can use .toggle() in jQuery
toggle() method toggles between hide() and show() for the selected
elements.
function toggleDiv(id) {
$('#'+id).toggle();
}
where id is the parameter you pass

How to add class to parent when child is visible?

I have a menu with a sub menu, when the sub menu is visible I want to add a class to parent list element.
<ul class="upper-menu">
<li>Item 1</li>
<li>Item 2</li>
<li class="parent-li">Item 3
<ul class="lower-menu">
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>Item 4</li>
</ul>
Basically, when the "lower-menu" is visible, I want to add another class to the list element "parent-li". Also when the lower-menu is not visible, I want to the remove the class from "parent-li". Is this possible in JQuery?
Thanks
You can do by following jquery script
if($('.lower-menu').is(':visible')){
$(this).parent().addClass("YourClass");
}
You can select the desired element by :visible pseudo class and then assign your class to its parent:
$(".lower-menu:visible").closest('.parent-li').addClass("test");
.test {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="upper-menu">
<li>Item 1</li>
<li>Item 2</li>
<li class="parent-li">Item 3
<ul class="lower-menu">
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul>
</li>
<li>Item 4</li>
</ul>

Hide an element that button is contained in using jQuery/Javascript

So I have some tiles that are arranged using a list of UL and then several LI tags.
I want to have a button inside one of the LI tags that will hide the whole tile when pressed.
Not sure how to go about it. Something like - this.('li').hide() but i know thats wrong lol.
Many thanks.
<ul class="sortable grid">
<li>Item 1</li><button onclick="hidePane()">Dismiss</button>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
</ul>
<script>
function hidePane(){
this.hide();
}
</script>
I want it to be dynamic. The intended use for this is to have a loop pulling in many tiles. So id like it to have a dismiss button on each and when clicked it will just hide tiles as you scroll.
Do not put elements outside li. In ul, first level elements inside should always be just li, so then inside the li you can put your button:
$('.sortable button').on('click', function() {
$(this).parent('li').hide();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="sortable grid">
<li>Item 1<button>Dismiss</button></li>
<li>Item 2<button>Dismiss</button></li>
<li>Item 3<button>Dismiss</button></li>
</ul>
A solution based on your code, using only JavaScript would imply to use event.target to get the current clicked button and its parent using parentElement and you would also use style.display = none; because there is no hide() method in pure JavaScript:
function hidePane(event) {
event.target.parentElement.style.display = 'none';
}
<ul class="sortable grid">
<li>Item 1<button onclick="hidePane(event)">Dismiss</button></li>
<li>Item 2<button onclick="hidePane(event)">Dismiss</button></li>
<li>Item 3<button onclick="hidePane(event)">Dismiss</button></li>
<li>Item 4<button onclick="hidePane(event)">Dismiss</button></li>
<li>Item 5<button onclick="hidePane(event)">Dismiss</button></li>
<li>Item 6<button onclick="hidePane(event)">Dismiss</button></li>
</ul>
You can try this:
Html code:
<ul class="sortable grid">
<li>Item 1 <br /><input type="submit" onclick="hidePane(this);"></li>
<li>Item 2 <br /><input type="submit" onclick="hidePane(this);"></li>
<li>Item 3 <br /><input type="submit" onclick="hidePane(this);"></li>
<li>Item 4 <br /><input type="submit" onclick="hidePane(this);"></li>
<li>Item 5 <br /><input type="submit" onclick="hidePane(this);"></li>
<li>Item 6 <br /><input type="submit" onclick="hidePane(this);"></li>
</ul>
JS code:
function hidePane(ref) {
$(ref).parent().hide()
}
It would be better if you do it by JQuery
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<ul class="sortable grid">
<li>Item 1<button class="dismiss"> Dismiss</button></li>
<li>Item 2<button class="dismiss"> Dismiss</button></li>
<li>Item 3<button class="dismiss"> Dismiss</button></li>
<li>Item 4<button class="dismiss"> Dismiss</button></li>
<li>Item 5<button class="dismiss"> Dismiss</button></li>
<li>Item 6<button class="dismiss"> Dismiss</button></li>
</ul>
Here is your JS
$(".dismiss").click(function () {
$(this).parent().hide();
});
I would recommend you to not to use html onclick() since it was considered a bad practice
[Why is using onClick() in HTML a bad practice?
You have to use jQuery: select the item by the id and then use the hide method.
$('#id_of_li').hide();
Try this code. It allows you to hide the li containing the button.
$("#hide").on('click',function(){
hidePane($(this).parent());
});
function hidePane(parent){
parent.hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="sortable grid">
<li>Item 1 <button id="hide">Dismiss</button></li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
</ul>

How to wrap all `ul` elements in `div` with JQuery automatically?

How to wrap all ul elements in div with JQuery automatically?
<div id="my_uls">
<ul>
<li>Item one</li>
<li>Item two
<ul>
<li>Item one of two</li>
</ul>
</li>
</ul>
</div>
But what I want:
<div id="my_uls">
<div>
<ul>
<li>Item one</li>
<li>Item two
<div>
<ul>
<li>Item one of two</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
You need to use .wrap():
$(function() {
$("ul").wrap("<div />");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_uls">
<ul>
<li>Item one</li>
<li>Item two
<ul>
<li>Item one of two</li>
</ul>
</li>
</ul>
</div>
Just use wrap function for this
<scrpt>
$(document).ready(function(){
var myuls = $("#my_uls").find("ul");
for(var i=0;i<myuls.length;i++){
$(myuls[i]).wrap("<div></div>");
}
});
</script>
$('#my_uls ul'), This will refer all your ul inside the div.
if you need the 1st ul only
$('#my_uls ul:not(#my_uls ul ul)').

Categories

Resources