Solved

jQuery code not working as expected

Posted on 2014-09-21
2
279 Views
Last Modified: 2014-09-21
Hi,

I have used a jQuery code show below from source and modified it so that when  the advanced search button in the content of the first tab is clicked it would show the table containing id adv-search and when that button is clicked again it would again hide the table containing id adv-search. But the script is not working as expected, i.e. when the advanced search button is clicked it shows the table and immediately hides it back without clicking the button again. which I don't want to happen. I want to hide the content to hide only when the advanced search button is clicked again.

Kindly let me know any inputs required. I am attaching the most of the code.

Can someone help me with identifying what is the issue?

Thanks in advance.
<div class="tabs">
					<ul class="tab-links">
						<li class="active" style="margin-left: 150px;"><a href="#tab1">PO's</a></li>
						<li><a href="#tab2" style="margin-left: 175px;">Device's</a></li>
					</ul>
					<hr>
					<div class="tab-content">
						<form id="tab1" class="tab active">
							<h2>List of PO's</h2>
							<input type="text">&nbsp;&nbsp;&nbsp;<button>Search</button>&nbsp;&nbsp;&nbsp;<button id="advsearch-btn">Advanced Search</button>
							<table id="adv-search">
								<td>123</td>
								<td>234</td>
								<td>234</td>
								<tr><td>asd</td></tr>
							</table>
						</form>
						<form id="tab2" class="tab">
							<p>Tab #2 content goes here!</p>
							<p>Test Conten.</p>
						</form>
					</div>
				</div>

Open in new window

$(document).ready(function() {
	$('.tabs .tab-links a').on('click', function(e)  {
		var currentAttrValue = $(this).attr('href');
		$('.tabs ' + currentAttrValue).slideDown(400).siblings().slideUp(400);// Show/Hide Tabs
		$(this).parent('li').addClass('active').siblings().removeClass('active');// Change/remove current tab to active
		e.preventDefault();
	});
	$('#adv-search').hide();//added this code myself
	$('#advsearch-btn').click(function(){//added this code myself
		$('#adv-search').toggle();//added this code myself
	});//added this code myself
});

Open in new window

0
Comment
Question by:Vipin Kumar
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40335204
test page : http://jsfiddle.net/xx1udomw/
replace :
      $('#advsearch-btn').click(function(){//added this code myself
            $('#adv-search').toggle();//added this code myself
      });//added this code myself
by :
      $('#advsearch-btn').click(function(evt){//added this code myself
                evt.preventDefault(); // else you're submitting your form and reloading the page
            $('#adv-search').toggle();//added this code myself
      });//added this code myself
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40335205
another way :
$('#advsearch-btn').click(function(){//added this code myself
            $('#adv-search').toggle();//added this code myself
            return false;
      });//added this code myself
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now