SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript function inside javascript

    Hi all,

    I have the following code but it isnt working and I dont know why.

    Basically what it is meant to do is that when you load the page and click on "Click here to change content" the div with the id content will change and will display " More content another link". When "another link" is click an alert message should appear saying hello jamie.

    Please could someone help

    Cheers

    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title>Untitled Document</title>
    		<script type="text/javascript">			
    			function changeContent(id,shtml)
    			{
    				if (document.getElementById || document.all)
    				{
    					var el = document.getElementById? document.getElementById(id): document.all[id];
    					if (el && typeof el.innerHTML != "undefined") el.innerHTML = shtml;
    				}
    			}	
    			
    			function myMessage(message)
    			{
    				alert(message)
    			}
    		</script>
    	</head>
    	<body>
    		<div id="sidebar" style="width:300px; border:1px solid black">
    			<a href="#" onclick="changeContent('content',' More content <br/> <a href=\'#\'  onclick=\'myMessage(\'hello jamie\')\'>another Link</a>');" /> 
    			Click here to change content
    			</a>
    		</div>	
    		<br/>
    		<div id="content" style="width:300px; border:1px solid black">
    		</div>
    	</body>
    </html>

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Improperly nested quotes:

    onclick=\'myMessage(\'hello jamie\')\'
    mmj

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks but how do I solve this problem?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Location
    Toronto, Canada
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not the best way IMO but it'll work. note that since you are using innerHTML to generate the code, you have to hardcode spaces between the text:

    <a href=\'#\' onclick=myMessage(\'hello&nbsp;jamie\')>another Link</a>');" />

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK great that works. So I now have
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title>Untitled Document</title>
    		<script type="text/javascript">
    			function changeContent(id,shtml) 
    			{
    				if (document.getElementById || document.all) 
    				{
    					var el = document.getElementById? document.getElementById(id): document.all[id];
    					if (el && typeof el.innerHTML != "undefined") el.innerHTML = shtml;
    				}
    			}
    			
    			function myMessage(message)
    			{
    				alert(message)
    			}
    		</script>
    	</head>
    	<body>
    		<div id="sidebar" style="width:300px; border:1px solid black">
    			<a href="#" onclick="changeContent('content',' More content <br/><a href=\'#\' onclick=myMessage(\'hello\r\njamie\')>another Link</a>');" /> Click here to change content</a>
    	</div>
    	<br/>
    	<div id="content" style="width:300px; border:1px solid black">
    	</div>
    </body>
    </html>
    How would I now get hello and jamie on two different lines so when the alert displays it looks like this
    Hello
    Jamie

    Cheers

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also I want to do
    <span>hello</span>
    jamie

    is that possible?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Location
    Toronto, Canada
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jamiecottonuk View Post
    Also I want to do
    <span>hello</span>
    jamie

    is that possible?
    You can't style an alert box. It's quite limited as to what you can do. Sounds like you should look into using css and a layer to create your own alert box that you can style as you see fit. Perhaps a simple show/hide layer thing. Will give you more flexibility than a javascript alert box.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •