CodeIgniter Forums
for each looa for document.getElementById - 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: for each looa for document.getElementById (/showthread.php?tid=78972)



for each looa for document.getElementById - gidgor256 - 04-02-2021

$('.remsubmit').click(function() {
                            var indexid= "<?=$indexid?>";
                            var name = $(this).prop('id');
                            var emailid = "<?=$emailid?>";
    var remarks="";
                           
                           
                          var x = document.getElementById("mySelect").selectedIndex;
                            var y = document.getElementById("mySelect").options;
                            remarks= y[x].text;
this insert onlydata from the first dropdown

is there a way i can loop through a crud list view and get each value of selected text/value of each dropdown based on the individual id of the listed view.


RE: for each looa for document.getElementById - demyr - 04-02-2021

well, is your code vanilla js or jquery? As far as I see, you have mixed them.
Please try to go with jquery (for example) :

not var x = document.getElementById("mySelect").selectedIndex;
but var x = $('#mySelect').val();  
or  var x = $('#mySelect option: selected').val();

Not because vanilla js is wrong, but jquery will be easier for you.

+

If you want to get a value of an id, it is easy as you saw. But if you want to get values from multiple select boxes sharing the same class, please
look for Jquery each function. It will help you to solve this matter.


RE: for each looa for document.getElementById - gidgor256 - 04-04-2021

(04-02-2021, 09:56 PM)demyr Wrote: well, is your code vanilla js or jquery? As far as I see, you have mixed them.
Please try to go with jquery (for example) :

not var x = document.getElementById("mySelect").selectedIndex;
but var x = $('#mySelect').val();  
or  var x = $('#mySelect option: selected').val();

Not because vanilla js is wrong, but jquery will be easier for you.

+

If you want to get a value of an id, it is easy as you saw. But if you want to get values from multiple select boxes sharing the same class, please
look for Jquery each function. It will help you to solve this matter.
PHP Code:
var remarks = [];
        $('#mySelect :selected').each(function() {
            remarks[$(this).val()] = $(this).text();
      
    console
.log(remarks);});


       

 
$('#mySelect').each(function(i){
   var remarks = $('#mySelect').val();

    console.log(remarks);}); 
still am not getting it any examples


RE: for each looa for document.getElementById - demyr - 04-08-2021

Could you please share your codes of this part (html-php + javascript part) ?