0

i had an onclick event for a button and it worked fine. now it doesnt and im trying to see why not. it worked for a little bit and i think i changed something, but the code seems correct. i think there may be some outside factors influencing the code possibly.

here's my code:

<script language="javascript">
function search(){

document.write("hi");
}

</form>
      <input type="button" value='s'  name='submit' id='submit' onClick='search();'>
</form>

<div id='searchdrop'>       
      stuff inside dropdown
</div>
6
  • you have jquery tagged, so i guess you using jquery. With jquery inline onclick is not working. check here jsfiddle.net/9th5g/.but i honestly don't know the reason. Commented Jun 9, 2011 at 4:15
  • @gowri What do you mean inline onclick doesn't work with jQuery? Commented Jun 9, 2011 at 4:27
  • @mazlix: onClick='search();' in field this is not working when i include jquery. if not it fires the search function correctly. Commented Jun 9, 2011 at 4:34
  • your jsfiddle works fine doesn't it.. i get the alert hi when i click the button, but please edit your post to make a space between the .but and the / Commented Jun 9, 2011 at 4:38
  • @mazlix: did you notice, i have in include jquery "no wrap(head)". That why it's works. when you change it to "onload". it won't. Commented Jun 9, 2011 at 5:09

5 Answers 5

5

Firstly, your markup is messed up. Correct it and then check if you still have problems. Shown below will just correct the markup you have shown, please make sure your entire markup is valid.

<script language="javascript">
function search(){
    document.write("hi");
}
</script> <!-- this was missing -->

<form> <!-- this was ending the form tag instead of starting -->
    <input type="button" value='s'  name='submit' id='submit' onClick='search();'>
</form>

        <div id='searchdrop'>

        stuff inside dropdown
        </div>
Sign up to request clarification or add additional context in comments.

Comments

3

this works

<html>  
<body> 
      <input type="button" value='s'  name='submit' id='submit' onClick='search();'> 
  <script type='text/javascript'> 
  function search(){
    document.write("hi");
}
  </script> 
</body> 
</html> 

Comments

1

Your form begins with a </form> rather than <form>. Could that be the problem?

1 Comment

i changed it and it still donest work. that was just poor copy and paste on my part into stackoverflow
0

You enigmatically decline to describe exactly what the "not working" part is, but I'll tell you right now that using document.write() inside an event handler like that is almost certainly not going to end up in a satisfactory user experience.

5 Comments

Well possibly, but (A) that's a crazy way to do a sanity check and (B) such an assumption presumes a degree of expertise not evinced by the user who posed this question.
@pointy that isnt the real code. i just used that to fill the space but ive pasted the real code below. im pretty sure its fine. what im trying to do is make a gmail style dropdown with a username:
@pointy var x=0; function decide() { if (x==0){ document.getElementById('popmenu').style.visibility= 'visible'; x++; } else { document.getElementById('popmenu').style.visibility= 'hidden'; x--; } }
Hey, @user780483 -- if that really is your name -- why don't you take a little time to read the Stackoverflow FAQ?
I believe in Google Chrome as least if you do a writeln() then your page will get overridden. For sanity checks i suggest using alert()
0
  • You haven't closed your <script> tag.
  • Your form starts with </form>

1 Comment

...which on further examination is what @Lobo was trying to say, I suppose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.