1

for the purpose to give the user the possibility to write in arabic i write the following code html :

<div id="clavier" style="display: none;" title="Clavier arabe">
   <table>
   <tr><td>
  <button value="ر" class="charachter">ر</button>
  <button value="ز" class="charachter">ز</button>
  <button value="ذ" class="charachter">ذ</button>
 <button value="د" class="charachter">د</button>
   <button value="خ" class="charachter">خ</button>
   <button value="ح" class="charachter">ح</button>
   <button value="ج" class="charachter">ج</button>
  <button value="ث" class="charachter">ث</button>
  <button value="ت" class="charachter">ت</button>
  <button value="ب" class="charachter">ب</button>
  <button value="ا" class="charachter">ا</button>
    <button value="mm" class="delete" style="width: 80px;">sup</button> </td></tr>
    <tr><td>
     <button value="ل" class="charachter" style="margin-left: 20px;">ل</button>
   <button value="ك" class="charachter">ك</button>
  <button value="ق" class="charachter" >ق</button>
 <button value="ف" class="charachter">ف</button>
   <button value="غ" class="charachter" >غ</button>
       <button value="ع" class="charachter">ع</button>
          <button value="ظ" class="charachter">ظ</button>
            <button value="ط" class="charachter">ط</button>
          <button value="ض" class="charachter">ض</button>
            <button value="ص" class="charachter">ص</button>
            <button value="ش" class="charachter">ش</button>
            <button value="س" class="charachter">س</button> 

             </td></tr>








  <tr><td>

    <button value="ى" class="charachter" style="margin-left: 40px;">ى</button>
   <button value="ئ" class="charachter">ئ</button>
   <button value="ؤ" class="charachter">ؤ</button>
   <button value="ء" class="charachter"  >ء</button>
   <button value="ة" class="charachter">ة</button>
   <button value="ي" class="charachter">ي</button>
   <button value="و" class="charachter">و</button>
   <button value="ه" class="charachter">ه</button>
   <button value="ن" class="charachter">ن</button>
   <button value="م" class="charachter">م</button>
   <button value="" class="clear">clear</button>

  </td></tr>



   <tr><td>
   <button value="" class="espace" style="height: 25px;width: 220px;margin-left: 60px;">espace</button>
   <button value="إ" class="charachter" >إ</button>
   <button value="أ" class="charachter">أ</button>
   <button value="آ" class="charachter">آ</button>
   <button value="ٱ" class="charachter">ٱ</button>

   </td></tr>
    </table>
    </div>

jquery :

$(".arab").live("click", function() {
    arabInput = $(this);
    $("#clavier").dialog({
        width: '43%'
    });
    $(".charachter").live("click", function() {
        alert($(this).val());
        var string = arabInput.val();
        alert(string + $(this).val());
        arabInput.val(string + $(this).val());
    });
    $(".delete").live("click", function() {
        var string = arabInput.val();
        arabInput.val(string.slice(0, -1));
    });
    $(".espace").live("click", function() {
        var string = arabInput.val();
        arabInput.val(string + " ");
    });
    $(".clear").live("click", function() {
        var string = arabInput.val();
        arabInput.val("");
    });
});​

the problem is that work fine for the first input , but onclick to second input i have the code executed twice and the same character is written twice for the third input it's tree times any idea , why i behaves like this ?

9
  • Try adding return false at the bottom of each click event to stop them from bubbling up. Commented Sep 11, 2012 at 15:41
  • 1
    Not sure why you're using .live. Aside from the fact that's it's deprecated in favor of .on, are you generating that HTML dynamically? Commented Sep 11, 2012 at 15:41
  • @FrançoisWahl i tried this not working Commented Sep 11, 2012 at 15:43
  • @Blazemonger no this code is not generated dynamiccaly Commented Sep 11, 2012 at 15:43
  • @Blazemonger: depend on the version of jQuery OP is using. on() was added in 1.7 so if OP uses a lesser version then live() was replaced by delegate in 1.4.2 Commented Sep 11, 2012 at 15:45

2 Answers 2

4

It's occurring the number of times you're clicking it because you are re-registering the event handler each time you click. You need to unbind the event handlers before adding new ones.

Sign up to request clarification or add additional context in comments.

Comments

3

as J Torres said, this code works fine the first time. But if you click on the input again, it adds new events to the buttons.

You can simple move the events outside of the $(".arab").click() method like this fiddle or remove the listeners on click like this fiddle.

$(".arab").click(function(){
  arabInput = $(this);
  $("#clavier").show();

});

$(".charachter").click(function()
  { alert($(this).val());
  var string = arabInput.val();
  alert(string+$(this).val());
  arabInput.val(string+$(this).val());
});

$(".delete").click(function()
  { var string = arabInput.val();
  arabInput.val(string.slice(0, -1));
});

$(".espace").click(function()
  { var string = arabInput.val();
  arabInput.val(string+" ");
});

$(".clear").click(function()
  { var string = arabInput.val();
  arabInput.val("");
});

or

$(".arab").live("click", function() {
    arabInput = $(this);
    $("#clavier").show();
    $(".charachter").unbind('click').on("click", function() {
        alert($(this).val());
        var string = arabInput.val();
        alert(string + $(this).val());
        arabInput.val(string + $(this).val());
    });
    $(".delete").unbind('click').on("click", function() {
        var string = arabInput.val();
        arabInput.val(string.slice(0, -1));
    });
    $(".espace").unbind('click').on("click", function() {
        var string = arabInput.val();
        arabInput.val(string + " ");
    });
    $(".clear").unbind('click').on("click", function() {
        var string = arabInput.val();
        arabInput.val("");
    });
});

8 Comments

add an other input with the same class attribute, you'll have get the problem i'm facing
@fatiDev Nope, works fine with the new method (putting the click event outside) : jsfiddle.net/HBYhg/3
@fatiDev Try this: jsfiddle.net/HBYhg/7. change .live( to .unbind('click').on(
The problem was that everytime you clicked on the input field, you would attach events to the buttons. So they would add up. After clicking 10 inputs fields, the buttons would execute 10 times because they had 10 listeners. Now, with "unbind" everytime you click on an input, unbind removes the click listener and right after adds a new one. So you only have 1 click listener executing the code.
@ComputerArts: As a side-note: if you are using on() use off() as it is the intended counterpart. Though unbind works off course.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.