Solved

jQuery code not working as expected

Posted on 2014-09-21
2
286 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

831 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