CodeIgniter Forums
[Solved] How to get Value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] How to get Value (/showthread.php?tid=66198)



[Solved] How to get Value - googlemy - 09-19-2016

How to get the Value of last selected option of HTML Multi Select ListBox using jQuery,

When I select "Item 1 and Item 3" and click button "Add to Cart" system select big value "3",
How to get the value selected item like this,
Example : Select Item 1 & Item 4 and get value "2".

Thanks you....


PHP Code:
<?php
    
echo form_open('', array(
        
'method' => 'post',
        
'class' => 'sky-form',
        
'id' => 'demoform',
        
'style' => 'border:none',
    ));
?>

<select id="selectList" multiple="multiple" size="10" name="qty">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    <option value="4">Item 4</option>
</select>

<script>
var demo1 = $('[name=qty]').bootstrapDualListbox();
$('#selectList option:selected').val();
</script>

<button type="button" class="btn add_to_cart" data-type="text"  data-pid='<?php echo $row['product_id']; ?>'>
        <i class="fa fa-shopping-cart fa-fw"></i>
        <?php echo translate('add_to_cart'); ?>
</button>

</form> 



Source :
http://www.jqueryscript.net/form/Responsive-jQuery-Dual-Select-Boxes-For-Bootstrap-Bootstrap-Dual-Listbox.html

Old script
http://www.jqueryfaqs.com/Articles/Get-the-Index-Value-and-Text-of-Last-Selected-Item-of-HTML-Multi-Select-ListBox-using-jQuery.aspx


RE: How to get Value - rtenny - 09-22-2016

(09-19-2016, 03:02 PM)googlemy Wrote: How to get the Value of last selected option of HTML Multi Select ListBox using jQuery,

When I select "Item 1 and Item 3" and click button "Add to Cart" system select big value "3",
How to get the value selected item like this,
Example : Select Item 1 & Item 4 and get value "2".

Thanks you....


PHP Code:
<?php
    
echo form_open('', array(
        
'method' => 'post',
        
'class' => 'sky-form',
        
'id' => 'demoform',
        
'style' => 'border:none',
    ));
?>

<select id="selectList" multiple="multiple" size="10" name="qty">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    <option value="4">Item 4</option>
</select>

<script>
var demo1 = $('[name=qty]').bootstrapDualListbox();
$('#selectList option:selected').val();
</script>

<button type="button" class="btn add_to_cart" data-type="text"  data-pid='<?php echo $row['product_id']; ?>'>
        <i class="fa fa-shopping-cart fa-fw"></i>
        <?php echo translate('add_to_cart'); ?>
</button>

</form> 



Source :
http://www.jqueryscript.net/form/Responsive-jQuery-Dual-Select-Boxes-For-Bootstrap-Bootstrap-Dual-Listbox.html

Old script
http://www.jqueryfaqs.com/Articles/Get-the-Index-Value-and-Text-of-Last-Selected-Item-of-HTML-Multi-Select-ListBox-using-jQuery.aspx

Not sure why you use that dual select plugin as you only have one select box. But here is what you do with a normal select box. Hope that helps

you bind the button with a click event to a function like this (i named the button "demo" but that can be any other name as well
Code:
$('#demo').click(function() {             
                var sel = $('#selectList').val();            
            }
            
        )
when you click the button the variable sel will be an array with all the selected values

https://jsfiddle.net/rtenny/c9ncmj7u/1/


RE: How to get Value - googlemy - 09-22-2016

(09-22-2016, 08:32 AM)rtenny Wrote:
(09-19-2016, 03:02 PM)googlemy Wrote: How to get the Value of last selected option of HTML Multi Select ListBox using jQuery,

When I select "Item 1 and Item 3" and click button "Add to Cart" system select big value "3",
How to get the value selected item like this,
Example : Select Item 1 & Item 4 and get value "2".

Thanks you....


PHP Code:
<?php
    
echo form_open('', array(
        
'method' => 'post',
        
'class' => 'sky-form',
        
'id' => 'demoform',
        
'style' => 'border:none',
    ));
?>

<select id="selectList" multiple="multiple" size="10" name="qty">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    <option value="4">Item 4</option>
</select>

<script>
var demo1 = $('[name=qty]').bootstrapDualListbox();
$('#selectList option:selected').val();
</script>

<button type="button" class="btn add_to_cart" data-type="text"  data-pid='<?php echo $row['product_id']; ?>'>
        <i class="fa fa-shopping-cart fa-fw"></i>
        <?php echo translate('add_to_cart'); ?>
</button>

</form> 



Source :
http://www.jqueryscript.net/form/Responsive-jQuery-Dual-Select-Boxes-For-Bootstrap-Bootstrap-Dual-Listbox.html

Old script
http://www.jqueryfaqs.com/Articles/Get-the-Index-Value-and-Text-of-Last-Selected-Item-of-HTML-Multi-Select-ListBox-using-jQuery.aspx

Not sure why you use that dual select plugin as you only have one select box. But here is what you do with a normal select box. Hope that helps

you bind the button with a click event to a function like this (i named the button "demo" but that can be any other name as well
Code:
$('#demo').click(function() {             
                var sel = $('#selectList').val();            
            }
            
        )
when you click the button the variable sel will be an array with all the selected values

https://jsfiddle.net/rtenny/c9ncmj7u/1/


Hi rtenny,

Thanks for your reply,

I already try your code but the output value is 1,2,3 & 4..
How to get value selected item for example, I select 1 & 3 and the output value "2" and I select 1,2 & 4 the output value "3".

Thanks


RE: How to get Value - Wouter60 - 09-22-2016

If I understand you correctly, you don't want the value, but the number of selected items.
Try this:

Code:
$('#demo').click(function() {            
    var sel = $('#selectList:selected').length;
    alert(sel);        
});



RE: How to get Value - googlemy - 09-23-2016

(09-22-2016, 10:41 PM)Wouter60 Wrote: If I understand you correctly, you don't want the value, but the number of selected items.
Try this:

Code:
$('#demo').click(function() {            
    var sel = $('#selectList:selected').length;
    alert(sel);        
});


Thanks you so much Wouter60,

Problem solve already...

I add "option" to get selected value.
Code:
jQuery( document ).ready(function($) {
       //code
    $('#demo').click(function() {             
    var sel = $('#selectList option:selected').length;
       alert(sel);    
    }
            
    )    
   });