0

the required in the script do not working

$("#s_postby").attr('required', false); // put the id of the input field that you whant required
$("#s_postby").attr('required', true); // put the id of the input field that you whant required

I make some radio field in my osclass theme on the item post!

    <div id="postby" class="property-ads-100">
      <label>
        <span class="required_fields">*</span>
        <?php _e('Posted By', 'ctg_housing'); ?>
      </label>
      <div class="item-post-postby-checkbox columns-0">
        <input  type="radio" name="s_postby" value="owner" id="s_postby0" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='owner') { echo 'checked="checked"'; }; ?>>
        <label for="s_postby0">
          <?php _e('Owner', 'ctg_housing'); ?>
        </label>
      </div>
      <div class="item-post-postby-checkbox columns-0">
        <input type="radio" name="s_postby" value="agent" id="s_postby1" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='agent') { echo 'checked="checked"'; }; ?>>
        <label for="s_postby1">
          <?php _e('Agent', 'ctg_housing'); ?>
        </label>
      </div>
      <div class="item-post-postby-checkbox columns-0">
        <input type="radio" name="s_postby" value="broker" id="s_postby2" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='broker') { echo 'checked="checked"'; }; ?>>
        <label for="s_postby2">
          <?php _e('Broker', 'ctg_housing'); ?>
        </label>
      </div>
      <div class="item-post-postby-checkbox columns-0">
        <input type="radio" name="s_postby" value="agency" id="s_postby3" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='agency') { echo 'checked="checked"'; }; ?>>
        <label for="s_postby3">
          <?php _e('Agency', 'ctg_housing'); ?>
        </label>
      </div>
    </div>

I hide this on some categories (the required in the script do not work!!) Why??

<script type="text/javascript">
$('#catId').change(function(){
    if( $('#catId').val() == "28" || $('#catId').val() == "29" || 
$('#catId').val() == "30" || $('#catId').val() == "31")
    {
        $("#postby").hide();  // change Posted By with ID you give to the div
    $("#s_postby").attr('required', false); // put the id of the input field that you whant required
    }else
    {
         $("#postby").show(); // change Posted By with ID you give to the div
    $("#s_postby").attr('required', true); // put the id of the input field that you whant required
    }
});
</script> 

What i need now it to make this radio fields required when show and not required wen hide! When show and required i need a error message Posted By: This field is required!

Hope someone can help me!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rhoda
  • 99
  • 9
  • I don't think `#s_postby` will match `#s_postbyNUMBER`. `$('input[name="postby"]).each` might be closer.. I don't JS much though, PHP brought me here. – chris85 Apr 17 '17 at 19:05
  • 1
    You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Apr 17 '17 at 19:05
  • Thanks i will try this and let you know if working – Rhoda Apr 17 '17 at 19:08

1 Answers1

2
element.required = true;

How to set HTML5 required attribute in Javascript?

Community
  • 1
  • 1
broodjetom
  • 276
  • 1
  • 11