Solved

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

Posted on 2011-09-21
4
251 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
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 450 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:gurvinder372
gurvinder372 earned 50 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

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

10 Experts available now in Live!

Get 1:1 Help Now