Stupid Javascript question
October 8, 2008 9:23 AM   Subscribe

I want to have data with spaces in it, in a link that is sent to a javascript function but it does not work. What am I doing wrong?

I want something like this to pass the data to a function, imagine inside a a link in the onClick=somefn('Data with a space'). It works fine for data without a space but doesn't work for data with a space.
posted by bytewrite to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
can we see what you're trying to do with the data? what, if any, messages do you get in your browser's error console?
posted by xbonesgt at 9:41 AM on October 8, 2008


Would this work?

<a href="#" onclick="alert('this data base spaces');">Click Me</a>

Click Me
posted by boba at 9:54 AM on October 8, 2008


There's not a lot of information to go by here, so please clarify your question if the below doesn't answer it.

If your your onclick action looks something like this:
<foo onclick="somefn('do something')">
and somefn() is not reformatting the string 'do something' before embedding it in the URL, you are effectively ending a malformed URL that looks like this:

http://www.example.com/bla.hxp?do something

You have to either encode the string (in this case, 'do something' will come out looking like do%20something, or perform a substitution (such as replace spaces with underscores or dashes: do_something or do-something).
posted by ardgedee at 9:59 AM on October 8, 2008


if your code is as in your question, then you're missing "s around the onclick value, as boba has, so the interpreter is only seeing as far as somefn('Data and getting confused.
posted by gregjones at 10:01 AM on October 8, 2008


I think gregjones has it- you need to put quotes around the value of your onClick parameter. I don't know why people are going off on malformed URL's, when there's no indication that the function generates any kind of URL.
posted by mkultra at 10:31 AM on October 8, 2008


Agree, I think gregjones sussed it out. Seems like you probably have this:
<a onclick=doSomething('foo bar baz')>
where instead what you need to have is this:
<a onclick="doSomething('foo bar baz')">
posted by letourneau at 10:50 AM on October 8, 2008


« Older Of the many, many Bible translations available...   |   NSFW: How do I feel comfortable with new partners? Newer »
This thread is closed to new comments.