Solved

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

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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 …
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…

792 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