I have an image that contains a data-keyword attribute. Can anyone show me how to get its string and show it in the input field when I click on the image? I can get the text link .special_field_link to work but not the image .image_keyword. (FIDDLE) The following code doesn't work:
HTML
<input id="a_input_id" type="text">
<a href="" class="special_field_link">@ABC<a>
<a href="" class="special_field_link">@DEF<a>
<img src="image.jpg" class="image_keyword" data-keyword="Hello" alt="pic">
JS:
$('.special_field_link,.image_keyword').click(function (e) {
e.preventDefault();
if($(this).is('.image_keyword')){
var keyword = $(this).data('keyword');
$('#a_input_id').val( ( $('#a_input_id').val()+" "+$(this).keyword.html() ).trim() );
}
else {
$('#a_input_id').val( ( $('#a_input_id').val()+" "+$(this).html() ).trim() );
}
});