GamaPL
GamaPL

Reputation: 97

Append input box with JQuery

I was trying to add text into field with id #shouttext, but i need to trim value from onclick event which is just after (' and also delete ') symbols.

So if i have

<img src="images/smilies/at.png" alt="" title="" class="smilie smilie_9 smilie_pointer" onclick="MyBBEditor.insertText(':at:');">

I want to get

:at:

and add it to input with #shouttext id. Below is my code, which doesn't work - and i think its blocked by defined onclick value, which i was trying to remove.

<script type="text/javascript">
$( document ).ready(function() {
$('.smilie').click(function()
{
   var s1,s1;
   s1=attr("onclick");
   s1=s1.replace('MyBBEditor.insertText(\'', '');
   s2=s1.replace('\')', '');
   $('.smilie').attr('onclick','').unbind('click')
   $('#shouttext').val($('#shouttext').val()+s2);
}) 
});
</script>

Thanks for any help in advance!

Upvotes: 0

Views: 68

Answers (2)

Santiago Hern&#225;ndez
Santiago Hern&#225;ndez

Reputation: 5636

you could try also:

var str = $(".smilie").attr("onclick").split("'")[1];

to get the :at: try it here: fiddle now you can use it here:

$('.smilie').unbind("click");
$('.smilie').click(function() {
    var str = $(".smilie").attr("onclick").split("'")[1];
    $('#shouttext').val($('#shouttext').val() + "" + str);
});

or how @eg_dac says, you can override the click event with $.on()

Upvotes: 1

eg_dac
eg_dac

Reputation: 701

instead of unbinding it in the click, you can do

$(".smilie").attr("onclick", ""); // clear the already declared attribute.

$(".smilie").on("click", function(){
    $('#shouttext').val($('#shouttext').val()+s2);
});

Example found here http://jsfiddle.net/meougz1L/

$("img").attr("onclick","");

$("img").on("click", function(){
    alert("click event overridden");
});

Upvotes: 0

Related Questions