Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jQuery code not working as expected

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn how to dynamically set the form action using jQuery.

839 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