Computer problems,Computer help
*AX SOFT>>>Programming & Design

Complicated document.write question in javascript?


Hi,

I'm trying to use document.write to display an anchor that calls a javascript
function with an argument when the anchor text is clicked.

The problem is when the anchor text is clicked I want the function that is invoked
to use the contents of a variable to specify the argument for that function.

For example, in the code below I want the function DeleteBook to be called
with one argument whose value is the string "id2", i.e. DeleteBook("id2") when
the anchor text is clicked.

I can't get it to work. I keep getting an error message that says "id2 is undefined"
when I click the anchor text.

Can anybody tell me how to do this?

Thanks,
Dave
--------------------------------------...
<HTML>
<BODY>

<SCRIPT>
function DeleteBook(sArg) {
alert("in DeleteBook, sArg = " + sArg)
}

var sId = "id2"
document.write("<A href='javascript:DeleteBook(" + sId + ")'>Delete Book</A>")
</SCRIPT>
</BODY>
</HTML>

The problem occurs with how you are using the variable "sld". When the link is written out, it actually writes out "id2" so Javascript thinks that it is a variable; which doesn't exist. You need to encase it in quotes to do what you want.

Practically speaking, you are going about this all wrong. It's bad form to use the javascript: protocol in a script for two reasons. On browsers with Javascript disabled, it will do nothing. Even worse, it won't die gracefully meaning that you will get some kind of standard browser error page.

The best way to do what you are attempting to do is to use the ONCLICK event handler in the A tag.
eg.
<.A HREF="#" ONCLICK="DeleteBook( sld); return false;">

The ONCLICK event handler creates an Anonymous function which you can readily find info on via Google.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Which code represents character data boxes and graphical symbols?

    Most English-only programs get by fine with ASCII but if you need to support non-English text you will want to take a look at Unicode. I don't think EBCDIC is used anywhere anymore. Progra...

  • VbScript - Help Me!?

    hi, use this coding: <script language="vbscript"> dim n(2) for i=0 to 2 n(i)=cint(inputbox("Enter 3 numbers")) next if n(0)<n(1) and n(0)<n(2) then if n(0...

  • Can we move files accross a unix filesystem?explain ur answer .?

    Yes, you can move files across UNIX file systems - If you move files with in a file system, the same inode number is maintained and works more as a rename - When you move files across file syst...

  • Inheritance and polymorphism help pleaseeeeee!!!!!!!!!?

    I don't think this is an inheritance problem I think the problem is in your constructor definition. I can't tell for sure without seeing your code but you most-likely passing your para...

  • How do i change the text color of the letters inside a table (HTML)?

    Hi there, Add style to your <td> tag... <td style="color: #FF0000;"> Or you can put the text in between div tags... <td> <div style="color: #FF0000;...

  • Arrange the following in descending order?

    each letter represents 4 bits. So any number that begins with 8-F is negative You can convert the numbers like so hex binary 8 1000 9 1001 A 1010 B 1011 ...

  • Can <tr> rows be styled or do you have to style seperate <td> cells?

    Yes, table rows, can be styled, but you can;t just specify any type of style. Then again, this is true for all HTML elements. I know you can specify a background for a table row, but not a borde...

  • Autorun function?

    All you need to do is put an autorun.inf file in the root. Check the source. Note that autorun is a request, not a demand. Users may have their computer set up to disregard autoruns.

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster