1

The checkbox value has been set dynamically through php

<ul>
    <li><input type="checkbox" name="menu[]" value="1">Politics         
        <ul>
            <li><input type="checkbox" name="submenu[1][1]" value="2">International</li>
            <li><input type="checkbox" name="submenu[1][2]" value="2">National</li>
        </ul>
    </li>
    <li><input type="checkbox" name="menu[]" value="2">Views</li>
    <li><input type="checkbox" name="menu[]" value="3">Sport            
        <ul>
            <li><input type="checkbox" name="submenu[3][1]" value="2">Volleyball</li>
            <li><input type="checkbox" name="submenu[3][2]" value="2">Cricket</li>
        </ul>
    </li>
</ul>

Now, can I remove checkbox value using jquery ? I want to remove value of only menu[] which has submenu[][]. For example, I am setting these menu[] checkboxes readonly using following way.

$('li:has(> ul)').find('[name="menu[]"]').click(false);

Now I want to add this trick, but doesn't work

$('li:has(> ul)').find('[name="menu[]"]').val('');

  // Here value is inserted using jquery
$('input[name="project"]').val("Good Fish");

  // But this is not working
$('li:has(> ul)').find('[name="menu[]"]').val('');

// This is working
$('li:has(> ul)').find('[name="menu[]"]').click(false);



$('[name="menu[]"] + ul input').click(function(){
		$(this).closest("ul").prev("input").prop("checked", ($(this).closest("ul").find("input:checked").length > 0));
	});
		$(document).on('submit','#form',function(e){
			e.preventDefault();
		if($("[name='menu[]']:checked").length == 0){
			alert('Missing Menu');
			return false;
		}
		else {
      alert('Success')
  }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<ul>
  <li><input type="checkbox" name="menu[]" value="1">Politics
    <ul>
      <li><input type="checkbox" name="submenu[1][1]" value="2">International</li>
      <li><input type="checkbox" name="submenu[1][2]" value="2">National</li>
    </ul>
  </li>
  <li><input type="checkbox" name="menu[]" value="2">Views</li>
  <li><input type="checkbox" name="menu[]" value="3">Sport
    <ul>
      <li><input type="checkbox" name="submenu[3][1]" value="2">Volleyball</li>
      <li><input type="checkbox" name="submenu[3][2]" value="2">Cricket</li>
    </ul>
  </li>
</ul>
<input type="text" name="project"><br/>

<input type="submit" value="submit">
</form>

6
  • added a snippet using your sample looks good to me Commented Jun 26, 2018 at 4:12
  • can you create fiddle? Commented Jun 26, 2018 at 4:24
  • @BhumiShah there is a snippet in the OP Commented Jun 26, 2018 at 4:47
  • @BhumiShah, Please see updated snippet, now I can clarify my problem. Commented Jun 26, 2018 at 4:55
  • @guradio, I've edited snippet, thanks Commented Jun 26, 2018 at 4:55

1 Answer 1

1

This will work

$('li:has(> ul)').find('[name*="menu"]').val('');

DEMO: https://codepen.io/creativedev/pen/zaJreN

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.