6 Replies - 360 Views - Last Post: 24 October 2013 - 10:02 AM

#1 josh771  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 13

how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 08:50 AM

I'm working on a web design, and i need to write a code for an image button who prints a string on a textbox.

Here is my code, but it doesn't work:


<form name = "frm">
<textarea name = "calc" rows="10" cols "40"></textarea>
<br>
<input type = "image" src="/images/deri.jpg" value ="fx" onclick="val1()">

//the javascript function

function val1()
{document.frm.calc.value += "f(x)dx";}

Help... Please..

Is This A Good Question/Topic? 0
  • +

Replies To: how to create a button image who prints a string on a textbox, on js

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 08:54 AM

Doesn't work.. how?
Was This Post Helpful? 0
  • +
  • -

#3 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 196
  • Joined: 19-October 13

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 09:04 AM

To prevent the default action you have to return false.
<input type="image" src="/images/deri.jpg" value="fx" onclick="val1();return false;">


Seems like the image button works like a submit button otherwise.
Was This Post Helpful? 0
  • +
  • -

#4 josh771  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 13

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 09:11 AM

View Postmodi123_1, on 24 October 2013 - 08:54 AM, said:

Doesn't work.. how?


thanks man, now it works, but why do i have to write "return false" on my sentence?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 09:38 AM

View Postjosh771, on 24 October 2013 - 06:11 PM, said:

but why do i have to write "return false" on my sentence?

because that’s how event cancellation in attributes works.
Was This Post Helpful? 0
  • +
  • -

#6 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 196
  • Joined: 19-October 13

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 09:40 AM

josh771 said:

thanks man, now it works, but why do i have to write "return false" on my sentence?


Returning false from an event attribute prevents the default action.

I have no experience with image buttons but it seems like it behaves like a submit button and submits the form when clicked on. When the form is submitted the page will be reloaded leaving the textbox empty. To prevent that from happen you can return false.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: how to create a button image who prints a string on a textbox, on js

Posted 24 October 2013 - 10:02 AM

but you could prevent the submitting completely by changing the HTML a bit:
<button type="button" id="myid">
	<img src="/images/deri.jpg">
</button>

myid.addEventListener("click", function(evt) {
	document.frm.calc.value += "f(x)dx";
});

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1