Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-07-2012, 06:18 PM   PM User | #1
merrymac
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
merrymac is an unknown quantity at this point
basic javascript issue: onclick script not working

hey everyone!!! im a total noob with javascript and am having trouble making this implementation work:

Code:
<button onclick='
<script type="text/javascript">
document.write("


Position: <input id="job_position_past_2" name="job_position_past_2" style="border:2px #12146B solid;" size="28" maxlength="50" type="text" />
<br/>
Company: <input id="job_company_past_2" name="job_company_past_2" style="border:2px #12146B solid;" size="28" maxlength="50" type="text" />
<br/>
")
</script>

' >Add job</button>
I'm running this in an html form on a local apache server. I'm just trying to include one of those "Add ___" buttons on forms that automatically insert another few input boxes. Javascript is enabled in the browser I'm using but I'm not sure if it is different because I'm on localhost.

WHen i click the button it says

"Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4 "
merrymac is offline   Reply With Quote
Old 08-07-2012, 06:22 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
You can't do an onclick like that. You can create a JavaScript function and use the onclick to evoke it, though.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 08-07-2012, 06:33 PM   PM User | #3
merrymac
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
merrymac is an unknown quantity at this point
oh didn't realize only functions were allowed. THanks!!!!
merrymac is offline   Reply With Quote
Old 08-07-2012, 07:19 PM   PM User | #4
merrymac
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
merrymac is an unknown quantity at this point
OK I've tried this now:

Code:
<script type="text/javascript">
function add_job(){
document.write("
Position: <input id=&quot;job_position_past_2&quot; name=&quot;job_position_past_2&quot; style=&quot;border:2px #12146B solid;&quot; size=&quot;28&quot; maxlength=&quot;50&quot; type=&quot;text&quot; />
<br/>
Company: <input id=&quot;job_company_past_2&quot; name=&quot;job_company_past_2&quot; style=&quot;border:2px #12146B solid;&quot; size=&quot;28&quot; maxlength=&quot;50&quot; type=&quot;text&quot; />
<br/>"
)
}
</script>
which i call with
Code:
<button onclick='add_job()' >Add job</button>
the quotes within quotes were messing it all up so I took the advice of another post and used the &quot; from html. Its still giving the same error though. Whats goin on!?
merrymac is offline   Reply With Quote
Old 08-07-2012, 07:21 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
Technically, you can do a little more than just functions, but the main point is that events like onclick are already JavaScript-related (ie, you don't have to start with "javascript:" in order for it to work) and a function would be much easier. Just write your function (for example, call it "doThis();") and in the button put onclick="doThis();" to evoke the function.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 08-07-2012, 07:23 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
Quote:
Originally Posted by merrymac View Post
the quotes within quotes were messing it all up so I took the advice of another post and used the &quot; from html. Its still giving the same error though. Whats goin on!?
document.write is not the best way. Use innerHTML. You have to escape the double quotes with a backslash "\".

" Position: <input id=\"job_position_past_2\" name=\"job_position_past_2\" style=\"border:2px #12146B solid;\" size=\"28\" maxlength=\"50\" type=\"text\" /> <br/>"
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Reply

Bookmarks

Tags
onclick add

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 AM.


Advertisement
Log in to turn off these ads.