Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery text(...) or html(...) set text, but then it immediately vanishes

Posted on 2011-09-21
4
Medium Priority
?
259 Views
Last Modified: 2012-05-12
With the code below, setting the div to .text("saved") or .html("saved") momentarily displays the "saved" text, but then it immediately reverts back to the previous text. The same is true if there is no previous text.

How do I get the contents of the div to stay with the new value?

 
<!DOCTYPE html>
 <html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="JQuery/jquery-1.6.4.min.js"></script>          
 <script type="text/javascript">
     $(document).ready(function () {
         $("a").click(function () {
             $("#savedMessage").text("Saved");
         });
     });                                    
 </script>                                                               
 </head>                                                                 
 <body>           
                                                   
    <a href="">Link</a>          
    <div id="savedMessage">hello</div>  
                                
 </body>                                                                 
 </html>

Open in new window

0
Comment
Question by:purplesoup
[X]
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
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 1800 total points
ID: 36572997
replace

<a href="">Link</a>    

with

<a href="#">Link</a>    
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36573016
here is the example

http://jsfiddle.net/CKK4V/1/

BR-Sudhindra-www.clicksperday.com
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 200 total points
ID: 36573025
or check this also
<!DOCTYPE html>
 <html>                                                                  
 <head>                                                                  
	<script src="jquery-1.4.2.min.js"></script>
 <script type="text/javascript">
     $(document).ready(function () {
         $("a").click(function () {
             $("#savedMessage").text("Saved");
         });
     });                                    
 </script>                               
 <style>
	a
	{
		color:blue;
		text-decoration:underline;
	}
 </style>
 </head>                                                                 
 <body>           
                                                   
    <a>Link</a>          
    <div id="savedMessage">hello</div>  
                                
 </body>                                                                 
 </html>

Open in new window

0
 

Author Comment

by:purplesoup
ID: 36573049
ansudhindra - yep - it is loading the page again, that or a javascript:return false will fix it - thanks.

ansudhindra - jsFiddle looks useful, but it doesn't actually seem to work for me - sorry. I click on the link and nothing happens.

gurvinder372 - nice, you can just respond to clicks on links without having any href defined... thanks !

0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

670 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