Welcome Guest, Not a member yet? Register   Sign In
on select change load value
#1

[eluser]il_dandi[/eluser]
Hi! In my view I've 2 fields


<tr>
<td align="right">commessa: </td>
<td>
<select name="idCommessa" id="idCommessa">
<option value=""></option>
&lt;? foreach ($commesse->result() as $row):?&gt;
<option value="&lt;?=$row->id?&gt;"&lt;? if($idCommessa==$row->id) echo " selected" ?&gt;>&lt;?=$row->titolo?&gt;</option>
&lt;? endforeach?&gt;
</select>
</td>
</tr>

<tr>
<td align="right">oggetto: </td>
<td>&lt;input name="oggetto" type="text" id="oggetto" size="40" maxlength="255" value="&lt;?=$oggetto?&gt;"&gt;&lt;/td>
</tr>

I would like to automatically load a value in "oggetto" input when I change the selected items in select field:

when I select a value in "idCommessa" I've to show in "oggetto" field the value: $row->titolo of commessa

Thanks
#2

[eluser]il_dandi[/eluser]
Please can you help me??
#3

[eluser]Georgi Budinov[/eluser]
Hello

You have to attach a function on the onchange event of the select element

onchange="onChangeSelection();"

If you use JQuery define the onChangeSelection function like this:

Code:
function onChangeSelection(){
     $('#oggetto').val($('#idCommessa option:selected').text());
}

If you do not use any js library use this:

Code:
function onChangeSelection(){
     var ogg = document.getElementById('oggetto');
     var sel = document.getElementById('idCommessa');
     var selectedIndex = sel.selectedIndex;
     ogg.value = sel.options[selectedIndex].text
}

Hope it is helpfull. Though I haven't tested it, but should be ok.
#4

[eluser]il_dandi[/eluser]
Thanks for your reply!!


I've do this:

<select name="idCommessa" id="idCommessa">
<option value=""></option>
&lt;? foreach ($commesse->result() as $row):?&gt;
<option value="&lt;?=$row->id?&gt;"&lt;? if($idCommessa==$row->id) echo " selected" ?&gt;>&lt;?=$row->titolo?&gt;</option>
&lt;? endforeach?&gt;
</select>


and in the same page (in my view) I've:

&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;aaaa&lt;/title&gt;
[removed][removed]

[removed]
function onChangeSelection(){
$('#oggetto').val($('#idCommessa optionConfusedelected').text());
}
[removed]

&lt;/head&gt;

but it doesn't works.... where is it my error?

Thanks
#5

[eluser]Georgi Budinov[/eluser]
This is a fast working example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;script type="text/javascript" src="jquery.js">&lt;/script>
&lt;/head&gt;
&lt;body&gt;

&lt;select id="tsts" onchange="onChangeSelection();">
<option value="0"></option>
<option value="1">1e</option>
<option value="2">2e</option>
<option value="3">3e</option>
</select>

&lt;input type="text" id="rsrs" name="rsrs" value="" /&gt;

&lt;script type="text/javascript">
function onChangeSelection(){
     $('#rsrs').val($('#tsts option:selected').text());
}
&lt;/script>

&lt;/body&gt;
&lt;/html&gt;
#6

[eluser]il_dandi[/eluser]
I've included this js

[removed][removed]

it's OK?

I still have the same problem
#7

[eluser]danmontgomery[/eluser]
If you're using jquery, why not go all the way?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;script type="text/javascript" src="jquery.js">&lt;/script>
&lt;script type="text/javascript">
$(function() {
   $('#tsts').change( function() {
     $('#rsrs').val($(this).find('option:selected').text());
   });
});
&lt;/script>
&lt;/head&gt;
&lt;body&gt;

<select id="tsts">
<option value="0"></option>
<option value="1">1e</option>
<option value="2">2e</option>
<option value="3">3e</option>
</select>

&lt;input type="text" id="rsrs" name="rsrs" value="" /&gt;

&lt;/body&gt;
&lt;/html&gt;
#8

[eluser]Georgi Budinov[/eluser]
Yes you could do that too. I wrote my post as fast reply, but however generally I am trying not to use the onload event for processing stuff like that, when possible of course.
#9

[eluser]il_dandi[/eluser]
Now it works!!

with $(function() {

can you tell me the differences between your first solution?

thanks
#10

[eluser]il_dandi[/eluser]
Now that I've resolved this, I've also this prolem:

http://ellislab.com/forums/viewthread/159054/

I think that it's similar but I don't know how to resolve it

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB