By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

how can I disable and enable input type?

idsanjeev
100+
P: 241
Hi
its urgent
how can enable and desable the input field with condition like
if rs("field")=1 then
desable
elseif re("field")=2 then
enable
end if
and that after chek if the input type is enable then manadatory field can't null
thanks
Jan 12 '08 #1
Share this Question
Share on Google+
33 Replies


acoder
Expert Mod 15k+
P: 16,027
With JavaScript, you'd use the disabled property. However, if you're going to set it whilst the page is loading with ASP, just include it within the tag:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" <% if it should be disabled, just print out: disabled="true" %> ...>
Re. the validation, please provide more details.
Jan 12 '08 #2

kunal pawar
100+
P: 297
Hi..
u can used directly in page
like
[HTML]<input type='control- text/button etc' name='' id='' <%if rs("field")=1 then%>
disabled=true <%elseif re("field")=2 then%>disabled=false<%end if%>>[/HTML]
Jan 14 '08 #3

idsanjeev
100+
P: 241
hi
I wants to use it in asp with vbscripts
and my need is to if selected option is floopy and cd then purpose Input filed is enable(show) ohterwiese purpose input field is(hide)
Thanks
Jan 16 '08 #4

gits
Expert Mod 5K+
P: 5,333
hi ...

i'm not familiar with vb-script but in case you want to set the disabled-attribute dynamically then use something like this (javascript):

Expand|Select|Wrap|Line Numbers
  1. var input_node = document.getElementById('your_input_node_id');
  2.  
  3. // to disable the box
  4. input_node.setAttribute('disabled', 'disabled');
  5.  
  6. // to make it readonly (text stays selectable)
  7. input_node.setAttribute('readonly', 'readonly');
  8.  
  9. // to enable it simply remove the attributes
  10. input_node.removeAttribute('disabled');
  11.  
  12. // or 
  13. input_node.removeAttribute('readonly');
  14.  
kind regards
Jan 16 '08 #5

idsanjeev
100+
P: 241
hello
where i have to use it can you sugest any position of code .
code would write within javascript tag
Jan 16 '08 #6

gits
Expert Mod 5K+
P: 5,333
hi ...

you have to assign an eventhandler to your select-element and call a function:

[HTML]<select name="my_name" onchange="toggle_state(this);">[/HTML]
now in the head of your page define the script-node:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function toggle_state(obj) {
  3.         // here we check the value of your select-box
  4.         var val = obj.value == 'floppy_cd';
  5.  
  6.         var input_node = document.getElementById('your_input_node_id');
  7.  
  8.         if (val) {
  9.             input_node.setAttribute('readonly', 'readonly');
  10.         } else {
  11.             input_node.removeAttribute('readonly');
  12.         }
  13.     }
  14. </script>
  15.  
kind regards
Jan 16 '08 #7

idsanjeev
100+
P: 241
Hello
My need is if option value is slected cd or floopy then show input filed if option selected is cartidge then hide input filed but when the above code is used no any chnges in forms so please help me in this
Jan 16 '08 #8

gits
Expert Mod 5K+
P: 5,333
you have to adapt the val-check to your option values of course.

are you sure that there are no changes? :) ... try to input something in case it should have been readonly ...

and please now tell clearly: should it be disabled, readonly or hidden?

kind regards

ps: please post your option html-code
Jan 16 '08 #9

idsanjeev
100+
P: 241
Hi sorry
I think i can't give you crrect need or little mistak
ok i am trying to decribe in brif
i have a form that has option select from table and click on add for requition of material that is in loop
my need is if select option is cd or floppy then automatic show input field for input purpose and if option is selected cartidge then hide input field and the requestion is submitted one by one in loop i can't able to do this.there are three field in select option cd,floppy and cartidge
code for select option
Expand|Select|Wrap|Line Numbers
  1.      <option Selected Value>Select Item</option>
  2. <%
  3. Set RSITEM = CON.EXECUTE("SELECT * FROM CONSUM_ITEMS ORDER BY Item_code")
  4. %>
  5. <%
  6. Do While Not RSITEM.EOF
  7. %>        <option Value="<%=RSITEM("Item_code")%>"><%=RSITEM("Item_desc")%></option>
  8. <%
  9.  
  10. RSITEM.MoveNext
  11. Loop
  12.  
thanks
Jan 16 '08 #10

gits
Expert Mod 5K+
P: 5,333
then we have to do some adaptions, here are the step-by-step hints:

1. give your input node an id and change the function accordingly ... so replace 'your_input_node_id' with your id

2. add the onchange-attribute the way i showed you

3. watch the magic ;)

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function toggle_state(obj) {
  3.         // here we check the value of your select-box
  4.         var val = obj.value == 'cartridge';
  5.  
  6.         var input_node = document.getElementById('your_input_node_id');
  7.  
  8.         input_node.style.visibility = val ? 'hidden' : 'visible';
  9.     }
  10. </script>
  11.  
kind regards
Jan 16 '08 #11

idsanjeev
100+
P: 241
Expand|Select|Wrap|Line Numbers
  1.  
  2. <b>Purpose( In case of CD/FLOPPY )</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  3.   <input type="text" name="purpose" id="purpose"  style="width:400px" maxlength="150"></td></tr>
  4.  
Expand|Select|Wrap|Line Numbers
  1.    <script language="javascript">
  2.   function toggle_state(obj) {
  3.         // here we check the value of your select-box
  4.         if var val = obj.value == 'cartridge'; 
  5.  
  6.         var input_node = document.getElementById('purpose');
  7.  
  8.         input_node.style.visibility = val ? 'hidden' : 'visible';
  9.     }
  10.  
  11.   </script>
  12.  
i chnges it like this is the incorrect if not then no magic
Jan 16 '08 #12

gits
Expert Mod 5K+
P: 5,333
did you add the onchange:

[HTML]<select name="my_name" onchange="toggle_state(this);">[/HTML]
to your select-box too?

kind regards
Jan 16 '08 #13

idsanjeev
100+
P: 241
hi
ya use this
[HTML]<option Value="<%=RSITEM("Item_code")%>" onchange="toggle_state(this);"><%=RSITEM("Item_des c")%></option>[/HTML]
Jan 16 '08 #14

gits
Expert Mod 5K+
P: 5,333
hi ...

there is an error here, i overlooked it the first time, sorry, your adaption has the following error:

Expand|Select|Wrap|Line Numbers
  1. if var val = obj.value == 'cartridge';
you don't need the if here ;)

kind regards
Jan 16 '08 #15

idsanjeev
100+
P: 241
hi
very fun today i am confuse how can solve it
my code is very long there are 286 line if you have some time then i send you my full code i there is any problems or i don't use the code provided in properway so please all works i don't know javascript
Jan 16 '08 #16

gits
Expert Mod 5K+
P: 5,333
Expand|Select|Wrap|Line Numbers
  1.  
  2. <b>Purpose( In case of CD/FLOPPY )</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  3.   <input type="text" name="purpose" id="purpose"  style="width:400px" maxlength="150"></td></tr>
  4.  
Expand|Select|Wrap|Line Numbers
  1.    <script language="javascript">
  2.   function toggle_state(obj) {
  3.         // here we check the value of your select-box
  4.         if var val = obj.value == 'cartridge'; 
  5.  
  6.         var input_node = document.getElementById('purpose');
  7.  
  8.         input_node.style.visibility = val ? 'hidden' : 'visible';
  9.     }
  10.  
  11.   </script>
  12.  
i chnges it like this is the incorrect if not then no magic
have a look at the line 4 ... you added an if here ... that is useless, so simply remove it and it should work ...

sending the code is not a good idea, may be you have a link to a testpage?

kind regards
Jan 16 '08 #17

idsanjeev
100+
P: 241
Hi Gits
I know that
but here is know file attachment option i am only online 1/2 hour tomorrow from morning 8 am ok
one more page but i send you only first page where first work is done
thanks
Jan 16 '08 #18

gits
Expert Mod 5K+
P: 5,333
hi ...

in your code you have:

Expand|Select|Wrap|Line Numbers
  1.       <td Width="15%"><select Id="SEL_ITEM" Name="SEL_ITEM" Size="1">
  2.         <option Selected Value>Select Item</option>
  3. <%
  4. Set RSITEM = CON.EXECUTE("SELECT * FROM CONSUM_ITEMS ORDER BY Item_code")
  5. %>
  6. <%
  7. Do While Not RSITEM.EOF
  8. %>        <option Value="<%=RSITEM("Item_code")%>" onchange="toggle_state(this);"><%=RSITEM("Item_desc")%></option>
  9.  
change in a way that the onchange is added to the select-element like:

Expand|Select|Wrap|Line Numbers
  1. <select Id="SEL_ITEM" Name="SEL_ITEM" Size="1" onchange="toggle_state(this);">
  2.  
the onchange mustn't be added to the option-element

kind regards
Jan 16 '08 #19

idsanjeev
100+
P: 241
hi ...

I Used your code but it not work when select option is selected automatic show input field and hide input field based on option
Jan 17 '08 #20

gits
Expert Mod 5K+
P: 5,333
please explain in detail how the option will get automatically selected? ... in case you do it serverside you have to set the visibility of the input-box accordingly ...

kind regards
Jan 17 '08 #21

idsanjeev
100+
P: 241
hi gits
i don't know about server side and client side
the main things wants to do when user login then a page is open with option to select item, quantity and submit to create requision for item he can add more then one item in same requision to add one by one so when he selected cd and floppy then show the input field if option is cartidge then hide show i don't know which type of code have to write client side or server side.
Jan 17 '08 #22

gits
Expert Mod 5K+
P: 5,333
hi ...

please load your page to a browser and view the source-code -> copy that source-code from the browser, save it as a txt-file and post it here or send me a pm with it ... i have to have a closer look at it ...

kind regards
Jan 17 '08 #23

idsanjeev
100+
P: 241
Hi Gits
Thanks for kind supports i attached here both page where these work is need
Jan 17 '08 #24

gits
Expert Mod 5K+
P: 5,333
hi ...

you posted the asp-code ... but i wanted to have a look at the page when it is loaded to the browser :) ...

let me give you a working example here that uses the code:

[HTML]<html>
<script type="text/javascript">
function toggle_state(obj) {
// here we check the value of your select-box
var val = obj.value == 'cartridge';

var input_node = document.getElementById('purpose');

input_node.style.visibility = val ? 'hidden' : 'visible';
}
</script>
<body>
<select onchange="toggle_state(this);">
<option selected="selected" value="">select something</option>
<option value="cartridge">cartridge</option>
<option value="CD">CD</option>
<option value="floppy">floppy</option>
</select>
<input type="text" id="purpose" style="visibility:hidden;"/>
</body>
</html>
[/HTML]
have a look at it ... the onchange HAS to be added to the select and HASN'T to be added to the option (like in formsan1.txt) ... as far as i could see you preselect an option with no value ... so we could adapt the function then ... and have a look at the input-field ... i hide it initially ... now when you change the selected value we get the input shown in all cases except cartridge ...

kind regards
Jan 17 '08 #25

idsanjeev
100+
P: 241
hi gits
sorry i have only these two page and one another that is submit requistion and generate report for requestion some other pages thats is departmentwise reports i know that i used it when another page it work fine but in these pages it not work why i don't know so what i have to do
thanks
Jan 17 '08 #26

idsanjeev
100+
P: 241
Thanks Gits
You are the good guide i find it after long discussion with u
and i am very upsate becouse some confussion in the page that was not develped by me but i have to given this work for midification in the requestion system for restriction .
please if you have time then i know little about server side code and client side code by You. you are best gui.
thanks again
Jan 17 '08 #27

gits
Expert Mod 5K+
P: 5,333
no problem ... glad to hear you got it working :) ... if you have more questions concerning client- and serverside coding please post a new thread to the forum ...

kind regards
Jan 17 '08 #28

idsanjeev
100+
P: 241
Hi gits
you know whats is the problems where we are dusscused last time problems is select option is number and i am compairing it with character so this is the region of our long descussion realy i am mad.
sorry again problems in show and hide
problems is when first choosen by user is cartidge and any thing it work fine but addition for next item it not work.and when add button goes to error your case is cd and floppy so please enter purpose that message is report in case of cd and floppy after this message it work again
on the status bar it show error massage and double click on it show detail the error on line 25 object required char=5 code=0
Jan 18 '08 #29

gits
Expert Mod 5K+
P: 5,333
the add-action submits the form ... is the page reloaded then? ... now i think i would really need a link to a testpage for futher assistence ... i need to see the data-flow and interactions etc. for my own ...

kind regards
Jan 18 '08 #30

idsanjeev
100+
P: 241
Hi gits
i send a new file attachment thats work fine but don't work when the hidden property is one time added in requistion other wise its work fine
the error thats displayed in status bar that is object require in javasript where function toggle_state is diclare and compare with one .
Jan 18 '08 #31

gits
Expert Mod 5K+
P: 5,333
hi ...

in your attached code you have:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="purpose" style="visibility:hidden; width:400px" maxlength="150" value=<%=porpose%>>
add an id-attribute since the toggle-function relies on it ...

kind regards
Jan 18 '08 #32

idsanjeev
100+
P: 241
Hi gits
Thanks for your kind time to time guidance
this code work fine

Expand|Select|Wrap|Line Numbers
  1. function toggle_state(obj) {
  2.     // here we check the value of your select-box
  3.    //var val = obj.value == 1; 
  4.  
  5.     if (obj.value == 1)
  6.  {
  7.  val=1;
  8.  }
  9.  else
  10.  {
  11.  val=0;
  12.  }
  13.  
  14.     var input_node = document.getElementById('purpose');
  15.  
  16.     input_node.style.visibility = val ? 'hidden' : 'visible';
  17.  
  18. }
  19. </script>
Jan 19 '08 #33

gits
Expert Mod 5K+
P: 5,333
Hi gits
Thanks for your kind time to time guidance
this code work fine

Expand|Select|Wrap|Line Numbers
  1. function toggle_state(obj) {
  2.     // here we check the value of your select-box
  3.    //var val = obj.value == 1; 
  4.  
  5.     if (obj.value == 1)
  6.  {
  7.  val=1;
  8.  }
  9.  else
  10.  {
  11.  val=0;
  12.  }
  13.  
  14.     var input_node = document.getElementById('purpose');
  15.  
  16.     input_node.style.visibility = val ? 'hidden' : 'visible';
  17.  
  18. }
  19. </script>
your adaption is definitly equivalent to the simple one line that you have 'outcommented' ... this couldn't have made a problem ... simply produces more lines of code and worse performance since 0 and 1 has to evaled to false/true in our assignment later on ... so it has to work with the

Expand|Select|Wrap|Line Numbers
  1. var val = obj.value == 1; 
  2.  
too?

kind regards
Jan 19 '08 #34

Post your reply

Sign in to post your reply or Sign up for a free account.