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.
βΊ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="">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'));
})
});