0

Is there any chance of firing click event when user clicks at end of text box for up to 30px padding-right? I have a background image at end of textbox. I have situation that I can not add background icon as div and append to right end of text box.

Kurkula
  • 6,386
  • 27
  • 127
  • 202

2 Answers2

1

Try this:

var text = document.getElementsByClassName('text')[0];
text.onclick = function(e){
    var el = e.currentTarget;
    var width = el.offsetWidth;
    var x = el.offsetLeft;
    var x1 = e.pageX;
    if(x1 > (x + width - 30)){   // 30 is padding-right value
        alert("hello");
    }
}

Working Fiddle

Mr_Green
  • 40,727
  • 45
  • 159
  • 271
0
<div class="textbox">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut feugiat vitae dui sit amet dignissim. Mauris laoreet ultrices libero. Fusce justo lacus, ultricies et egestas ultrices, ultrices et metus. Aenean at rutrum nibh, imperdiet interdum lectus. Pellentesque in neque luctus, pulvinar dui non, lobortis urna. Nulla imperdiet congue felis quis euismod. Pellentesque tempus ornare porta. Ut sit amet nibh non dui hendrerit placerat at sit amet tellus. Phasellus ut est justo. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aenean sit amet urna quis quam facilisis tempor. Sed ut eleifend risus, in elementum ligula.
</div>

$('.textbox').append('<a href="#">Press me</a>');
$('.textbox a').click(function() {
  alert( "Handler for .click() called." );
});

http://jsfiddle.net/KGy48/

MentalRay
  • 334
  • 1
  • 6