0

I am using javascript to change the value of text from select to selected.

Now I want to add in cart which have selected text. Please advise. Thanks in advance.

PHP CODE :

<a class='btn slct' href='javascript:void(0)' >Select</a>

Javascript Code :

<script>

$(document).ready(function() {

 $('.slct').click(function() {
$(this).text($(this).text() == 'Select' ? 'Selected' : 'Select');


        });
    });

</script>
Dil85
  • 176
  • 1
  • 1
  • 14
Test
  • 45
  • 1
  • 8

3 Answers3

2
$('.btn').click(function(){

    var linkText = $(this).val();
    if(linkText = 'Select'){
        $(this).html('Selected');
   }
});

I think you should try this to solve your problem.

Izuka
  • 2,572
  • 5
  • 29
  • 34
aizaz
  • 21
  • 3
0

use

$(this).val($(this).text() == 'Select' ? 'Selected' : 'Select');

instead of

$(this).text($(this).text() == 'Select' ? 'Selected' : 'Select');
guradio
  • 15,524
  • 4
  • 36
  • 57
Nadeem Shaikh
  • 289
  • 2
  • 13
0
      $(document).ready(function() {
      $('.slct').click(function() {
       var value = [];
       var text = [];
            $(".slct:checked").each(function() {
                value.push(this.value);
                text.push('Selected');
            });

    $.ajax({
    type:"POST",
    url: 'edit.php',
    data: "value ="+ value+"&text="+text ,
    dataType: 'text',
    async: false,
    cache: false,
    success: function ( result )  {
        window.location.reload();
    }
    });
       });
     });

change checkboxname as your name

  $('input[name="checkboxname"]:checked').each(function() {
   $(this).val($(this).text() == 'Select' ? 'Selected' : 'Select');
   $(this).html('Selected');
  });

it will return total check count

$('.slct').click(function () {
var check = $('.slct').find('input[type=checkbox]:checked"]').length;
alert(check);
return false;
}):

you can also use this

var check = $('.slct').find('input[type="checkbox"]:checked').length;
alert(check);
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
  • how it will return total checked values and can i show them to cart on next page. Pl advise – Test Mar 08 '17 at 10:08
  • total check count not work? next page mean send this value in php script for action? – Shafiqul Islam Mar 08 '17 at 10:18
  • There is link Check selected Values. When user click on that link I want to redirect him to new page where I want to show all the selected values by him. Can we do this? Like add to cart system – Test Mar 08 '17 at 10:25
  • so send this all selected data using get method then in that page you find all data or add hidden form and submit it in that link . then it will solve this – Shafiqul Islam Mar 08 '17 at 10:35
  • but i am not using any form in this. Can we do this with any other method? – Test Mar 08 '17 at 10:55
  • ok use ajax method send save it session and response redirect your page – Shafiqul Islam Mar 08 '17 at 10:57
  • http://stackoverflow.com/questions/42555389/get-javascript-variable-value-in-php-variable/42556181#42556181 – Shafiqul Islam Mar 08 '17 at 10:58
  • Can you plz give me the full code how to use this? Thanks – Test Mar 08 '17 at 12:49
  • i am busy. i will do it few hour later. if you want this – Shafiqul Islam Mar 08 '17 at 12:50
  • Ok great... and I am using this in php while loop. Select text is in all rows. Can we do with text whenever user click select, Then text becomes selected till this its working. after that i want to show how many are selected and which are selected on next page. When user click on link to see the selection which is made by him – Test Mar 08 '17 at 12:54
  • ok show me your checkbox form structure, you want to checkbox value with level text? – Shafiqul Islam Mar 08 '17 at 13:12
  • I am not using form. – Test Mar 08 '17 at 13:42
  • Can we do with text whenever user click select? not clear which text you send? selected $(this).val($(this).text() == 'Select' ? 'Selected' : 'Select'); – Shafiqul Islam Mar 08 '17 at 13:47
  • have to use text with checkbox? and have to use all three functions which you have mentioned – Test Mar 08 '17 at 14:15
  • check first function and check alert data is ok then if ok use only this – Shafiqul Islam Mar 08 '17 at 14:18
  • yes it shows alert data is on but how can i get these values on next page with click on link or button? – Test Mar 08 '17 at 14:22
  • check this it will help you. use ajax and set session then you can use any page http://stackoverflow.com/questions/42555389/get-javascript-variable-value-in-php-variable/42556181#42556181 – Shafiqul Islam Mar 08 '17 at 14:24
  • check updated first function then create edit.php something like that and set value in session and click link or button then go this page then print_r($_SESSION); – Shafiqul Islam Mar 08 '17 at 14:27
  • can u plz help how i can use this with your function and and checkbox and how to use this in link. It would be great help for me. – Test Mar 08 '17 at 14:30
  • please give me your html or just give me screenshot which type you want – Shafiqul Islam Mar 08 '17 at 14:34