Select this Shop ...">

Submit value using jquery

how to send value using jquery? I have tried this.

<span onclick="doSomething()" value="3">Select this Shop</span>
<input class="my-shop" value="">
<script>

function doSomething(){

  var text = $( this ).val();

  $( ".my-shop" ).val( text );
}
</script>

My code does not work, but I do not know why. Thank you for your help.

+4
source share
3 answers

β–ΊThe valueone you specified in spanis an attribute. Therefore you need to get the attribute with .attr().

Working demo

function doSomething(elem){
  var text = $(elem).attr('value');
  $( ".my-shop" ).val( text );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span onclick="doSomething(this)" value="3">Select this Shop</span>
<input class="my-shop" value="">
Run codeHide result
+1
source

You can try the same features in different templates. Demo

<span value="3">Select this Shop</span>
<input class="my-shop" value="">
$(document).ready(function() {
 $('span').click(function(e){
  $( ".my-shop" ).val($(this).attr('value'));
 })
});

If you have a lot spanon the same page and want to target a specific one span, you can add a class for the jquery selector.

    <span  value="3" class="unique">Select this Shop</span>
   <input class="my-shop" value="">
    $(document).ready(function() {
     $('.unique').click(function(e){
      $( ".my-shop" ).val($(this).attr('value'));
     })
    });
+1
source

doSomething() this . , console.log(this) .

One way to implement this is to use jQuery selectors.

function doSomething(){
  var text = $('span').attr('value');
  $( ".my-shop" ).val( text );
}

You can find more about jQuery selectors at this link .

0
source

All Articles