Jump to content

Need Help Rewriting This


kensbeijing
 Share

Recommended Posts

I have a javascript function inside a form input:

<input name="smile" onclick="document.postwall.message.value=document.postwall.message.value.concat('[smile]');" src="img/smi_1.gif" title="smile" type="image" />

What this does is add the word "[smile]" into a textarea called "message".However I have 16 of these codes, each with the same "onclick" javascript actions but with different "[smile]"s.Would it be possible to write a single javascript function at the top of the page doing the same function, but putting "[smile]" as a variable?

Link to comment
Share on other sites

You could do something like this, just send the text from the function call:

<script type="text/javascript">function putText(where,what) {	where.value = where.value + what;}</script><input name="smile" onclick="putText(document.postwall.message,'[smile]');" src="img/smi_1.gif" title="smile" type="image" />

Or perhaps if you want use an array of text's and pass the text items array index number:

<script type="text/javascript">function putText(where,what) {	var txtArr = ['[smile]', '[frown]', '[grimace]', '[cry]'];	where.value = where.value + txtArr[what];}</script><input name="smile" onclick="putText(document.postwall.message,0);" src="img/smi_1.gif" title="smile" type="image" />

Link to comment
Share on other sites

Time to get with best practices, guys. Stop accessing page elements with names. An element you need to access should have an id. Get a reference to it using document.getElementById("the_id"). This is basic.
I have no idea how to use javascript, and I don't plan on going near it coz it's just such a mess. What do you mean by that getelementbyid thing?
Link to comment
Share on other sites

What he means is, if "message" is the name of an element being used here, give it an id="message" and then change the script to be like:

<script type="text/javascript">function putText(whereId,what) {	var txtArr = ['[smile]', '[frown]', '[grimace]', '[cry]'];	var elem = document.getElementById(whereId);	elem.value = elem.value + txtArr[what];}</script>Then change the onclick function call to pass the id as a string:<input name="smile" onclick="putText('message',0);" src="img/smi_1.gif" title="smile" type="image" /

>

Link to comment
Share on other sites

What he means is, if "message" is the name of an element being used here, give it an id="message" and then change the script to be like:
<script type="text/javascript">function putText(whereId,what) {	var txtArr = ['[smile]', '[frown]', '[grimace]', '[cry]'];	var elem = document.getElementById(whereId);	elem.value = elem.value + txtArr[what];}</script>Then change the onclick function call to pass the id as a string:<input name="smile" onclick="putText('message',0);" src="img/smi_1.gif" title="smile" type="image" /

>

Doesn't work, am I right in saying that the 0 after 'message' is the first one in the array which is ([smile]). Also the actual button is in a different form than the textarea (message).
Link to comment
Share on other sites

Yes, the 0 is being used as an array index, which in this case corresponds to [smile]. The beauty of using IDs is that they can be accessed anywhere, so it doesn't matter that the button is not in the form.When it doesn't work, what do you mean?I hope you're developing in Firefox, which has a very nice built-in error console that reports everything that goes wrong, usually with the line number of the spot where the code broke. You access it under the Tools menu. It reports bad HTML and CSS as well. Usually it gets pretty cluttered as you surf the web, since most sites have tons of errors, so It's best to clear it before loading your test page. Seriously, this little tool shaves hours off development time. People often post problems here, and when I click on their link, the error console tells me what's wrong immediately. I post back and they think I'm a genius, but all I'm doing is using a free tool. It's crazy not to.Maybe you can post what your code currently looks like? Any little typo can make things toast. A link would be better, if you have one.(I trust you caught the misplaced angle bracket at the end of astupidname's code?)

Link to comment
Share on other sites

Any little typo
(I trust you caught the misplaced angle bracket at the end of astupidname's code?)
Lol, I'll bet that's the whole problem! Oops. Been having some trouble with mouse (fresh batteries and a bit hyper-sensitive) when copied and pasted from my text editor. So you see, it's not my fault, darn mouse... strange how my eyes don't work though... :-)To expand a bit on what Deirdre's Dad is saying about developing in Firefox, I totally love it. You must get Firefox and get the Firebug and Html Validator add-on's for Firefox (all free). They are tools I refuse to do without.
Link to comment
Share on other sites

Lol, I'll bet that's the whole problem! Oops. Been having some trouble with mouse (fresh batteries and a bit hyper-sensitive) when copied and pasted from my text editor. So you see, it's not my fault, darn mouse... strange how my eyes don't work though... :-)To expand a bit on what Deirdre's Dad is saying about developing in Firefox, I totally love it. You must get Firefox and get the Firebug and Html Validator add-on's for Firefox (all free). They are tools I refuse to do without.
I'm not getting any errors :), so is the name of the textarea meant to be "message"?Here's what I have:
<script type="text/javascript">function putText(whereId,what) {	var txtArr = ['[smile]', '[cool]', '[laugh]', '[embar]', '[foot]', '[frown]', '[cry]', '[inno]', '[kiss]', '[money]', '[seal]', '[surp]', '[tongue]', '[confus]', '[wink]', '[angry]'];	var elem = document.getElementById(whereId);	elem.value = elem.value + txtArr[what];}</script><input name="smile" onclick="putText('message',0);" src="img/smi_1.gif" title="smile" type="image" />

Link to comment
Share on other sites

The name doesn't matter for Javascript, only the ID. The name is used when you submit the form, Javascript uses the ID.<textarea id="message" ....Also, IDs must be unique, if you have 2 elements on the page with the same ID you'll run into problems.

Link to comment
Share on other sites

The name doesn't matter for Javascript, only the ID. The name is used when you submit the form, Javascript uses the ID.<textarea id="message" ....Also, IDs must be unique, if you have 2 elements on the page with the same ID you'll run into problems.
Oh I see, I thought getelementid was to get the name of the object. And yes I had a div with id "message" so that would have got in the way. Thanks.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...