Solved

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

Posted on 2011-09-21
4
258 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

632 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