Hello! A problem such as passing the value of a checkbox if it is a value with a space:

An example I need to get images word , and I get the images below

$(document).ready(function() { $("button#renameSubmit").bind("click", function show() { var pathUpload = $('input[type="checkbox"]:checked').val(); alert(pathUpload); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" id="pathUpload" name="pathUpload[]" value="images"> <span class="text-folder">images word</span> <br/> <button type="button" id="renameSubmit" value="1">отправить</button> </form> 

    2 answers 2

    The checkbox has a value attribute, which you get.

    Option 1: change attribute

     $(document).ready(function() { $("button#renameSubmit").bind("click", function show() { var pathUpload = $('input[type="checkbox"]:checked').val(); alert(pathUpload); }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" id="pathUpload" name="pathUpload[]" value="images word"> <span class="text-folder">images word</span> <br/> <button type="button" id="renameSubmit" value="1">отправить</button> </form> 

    Option 2: get the text

     $(document).ready(function() { $("button#renameSubmit").bind("click", function show() { var pathUpload = $('input[type="checkbox"]:checked').next('span').text(); alert(pathUpload); }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" id="pathUpload" name="pathUpload[]" value="images"> <span class="text-folder">images word</span> <br/> <button type="button" id="renameSubmit" value="1">отправить</button> </form> 

      First: instead of $().bind() use $().on() .

      You have the same jQ 2.1.1

      And secondly: why do you need a named function in the handler?

        ... function show() ... 

      can be replaced simply by

        ... function() ... 

      And thirdly:

       var pathUpload = $('input[type="checkbox"]:checked').val(); 

      should get the value from the value attribute of this field. This attribute is missing. You can get the contents of the <span> element like this:

       var pathUpload = $('input[type="checkbox"]:checked').next().text();