-2

I need to disable/enable a button depending on what option the user choose on the dropdown menu. Using the code below it will disable the button if one trip is paid even thought I select a different trip it will stay disabled
code:

<div class="form-group">
<select onchange="showOptions(this)" id="tripSelect">
    <option value="" disabled selected align="center">-- Choose Trip --</option>
<?php $z = 0; ?>
@foreach($event_groups as $event_group)
    <?php
        ...
    ?>
    @if($event) 
        <option class="dd" value="{{$optionID}}">{{$event->trip_name}}</option>
                                            // some trips are paid
    @endif
@endforeach
</select>
</div>&emsp;
    <?php
        ...
    ?>
@if ( EUser::noOneHasPaid( $booking_id ) ) // if not yet paid then enable
    <a class="btn-success invitation" id="add">Add Friends</a>
@else                                      //else disable button
    <button class="btn-success" disabled>Add Friends</button>
@endif

Please help me.

Newbie
  • 47
  • 6
  • Possible duplicate of [How to get jQuery dropdown value onchange event](http://stackoverflow.com/questions/19922729/how-to-get-jquery-dropdown-value-onchange-event) – Rajesh Dec 27 '16 at 08:31
  • @Rajesh: JavaScript doesn't imply jQuery. – icktoofay Dec 27 '16 at 08:31
  • This mentioned link uses jQuery to bind event but you can do without it as well. Just use `element.addEventListener` – Rajesh Dec 27 '16 at 08:32
  • add php tag to the question – Developer Dec 27 '16 at 08:32
  • @icktoofay please read my second comment – Rajesh Dec 27 '16 at 08:32
  • There is so much hate in my post :D – Newbie Dec 27 '16 at 08:40
  • @Newbie, first of all you mix PHP with your HTML. PHP is fired before HTML which makes your `if else` useless unless your page is reloaded and you use the `localstorage` of your browser. Second I should mention that your button has no `ID` you can't access it directly. Furthermore you should reconsider your codestyle... Oh and using Angular with prefilled variables from PHP is not a good idea at all, you will run into some timing problems sooner or later. – Megajin Dec 27 '16 at 08:47

2 Answers2

0

use this - this will convert your html to javascript http://www.accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/

  • Changing the whole page to JS does not exactly seem the right way to approach this. A simple update of the button's attribute in the onChange event of the combo will do. – rob2universe Dec 27 '16 at 08:46
0

The button needs a name or ID.

onchange="if (tripSelect.value == "(something)" { buttonnameorid.disabled=false; }"
Ben Davis
  • 102
  • 1
  • 6
  • Its a bad practice writing inline event handlers. Anyone can change it using Dev tools and break your site. I'd suggest using `.addEventListener` – Rajesh Dec 27 '16 at 08:40