List Info

Thread: JavaScript function issues: I can't figure it out.




JavaScript function issues: I can't figure it out.
country flaguser name
United States
2007-02-25 18:33:16
Hello,
I want to click a button which sends the name of an input
field to the
function.  From there I want the function to take the value
of that
input field, split it by the "/" symbol, take the
last field in the
array, check to see if it is already part of a different
textarea
field name extra_field[4], and if not, add it to the end of
that
field.  Here is what I have so far:

<form name="new_product">
My input field:
<input type=text name=test><button
onclick="colors(test)">run colors
on test</button>

<script language="JavaScript">
<!--
function colors(blob) {
  var
blob2=document.forms["new_product"].blob.value;
  var blob3=blob2.split("/");
  iname=blob3[(blob3.length-1)];
  if(blob=="clear")
document.forms["new_product"].extra_field[4].value
="";
  else {
    if(document.getElementById('extra_field[4]').value ==
"") //
nothing in there
 
document.forms["new_product"].extra_field[4].value
="runpictures:," +
iname;
   
if(document.getElementById('extra_field[4]').value.search(in
ame)
== "-1")//not found so add
 
document.forms["new_product"].extra_field[4].value
=document.forms["new_product"].extra_field[4].valu
e
+ "," + iname;
  }
}

//-->
</script>

<textarea 
name="extra_field[4]"></textarea>
</form>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "JavaScript Forum" group.
To post to this group, send email to
JavaScript-Informationgooglegroups.com
To unsubscribe from this group, send email to
JavaScript-Information-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/JavaScript-Information
?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: JavaScript function issues: I can't figure it out.
country flaguser name
United States
2007-02-27 07:32:44
If that is exactly your code, try calling the function in
the on click
event with the name of your input field wrapped in quotes
->
onclick="colors('test')"
You shuld be able to read out the value then.
Also in queries that return an index and -1 if nothing is
found i'd
rather check for the integer value. So it would be
if(string.search(searchString) < 0). It is easier to read
in my
oppinion and also it is comparing an integer to an integer
and not to
a string as you did, wrapping the -1 in quotes.

Just a hint. You are writing code that gets transmitted, so
try to
keep it short. You have several times
ocument.getElementById('extra_field[4]') - why not save it
into a
variable and then use that variable later on. That would
save you lots
of writing and bandwith too (well, that is if your scripts
get
bigger).
Example:

var extraField =
document.forms["new_product"].extra_field[4];
if(blob=="clear") {
	extraField.value="";
} else {
	if(extraField.value == "")
	{ // nothing in there
		extraField.value="runpictures:," + iname;
	}
	if(extraField.value.search(iname) < 0)
	{ //not found so add
		extraField.value += "," + iname;
	}
}

You see, the code is shorter bt still more readable.

Hope that helps
Torsten

On Feb 26, 1:33 am, "Steve" <ipodxt...gmail.com> wrote:
> Hello,
> I want to click a button which sends the name of an
input field to the
> function.  From there I want the function to take the
value of that
> input field, split it by the "/" symbol, take
the last field in the
> array, check to see if it is already part of a
different textarea
> field name extra_field[4], and if not, add it to the
end of that
> field.  Here is what I have so far:
>
> <form name="new_product">
> My input field:
> <input type=text name=test><button
onclick="colors(test)">run colors
> on test</button>
>
> <script language="JavaScript">
> <!--
> function colors(blob) {
>   var
blob2=document.forms["new_product"].blob.value;
>   var blob3=blob2.split("/");
>   iname=blob3[(blob3.length-1)];
>   if(blob=="clear")
>
document.forms["new_product"].extra_field[4].value
="";
>   else {
>     if(document.getElementById('extra_field[4]').value
== "") //
> nothing in there
>
>
document.forms["new_product"].extra_field[4].value
="runpictures:," +
> iname;
>    
if(document.getElementById('extra_field[4]').value.search(in
ame)
> == "-1")//not found so add
>
>
document.forms["new_product"].extra_field[4].value
=document.forms["new_prod
uct"].extra_field[4].value
> + "," + iname;
>   }
>
> }
>
> //-->
> </script>
>
> <textarea 
name="extra_field[4]"></textarea>
> </form>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "JavaScript Forum" group.
To post to this group, send email to
JavaScript-Informationgooglegroups.com
To unsubscribe from this group, send email to
JavaScript-Information-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/JavaScript-Information
?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )