0

Here is a jsfiddle example of the problem I've been working on:

http://jsfiddle.net/YD6PL/61/

HTML:

<input type="text">
<input type="text">
<input type="text">
<div>
<button class="buttons">c</button>
<button class="buttons">a</button>
<button class="buttons">t</button>
</div>

JS:

$(document).ready(function () {

    $('input').click(function(){
        $(this).addClass('active').siblings('.active').removeClass('active')
    });

    $(".buttons").click(function () {
        var cntrl = $(this).html();
        $('input.active').append(cntrl);
    });
});

I'm unable to get the button click to insert the clicked character into the textbox in the jsfiddle example. Any idea how to fix this?

2
  • 2
    If you replace append with val, this works for me. By works, i mean clicking a text box and then clicking a button inserts the buttons value into the text box. Commented Sep 6, 2014 at 22:31
  • jsfiddle.net/lordloh/qgbtydgj Commented Sep 6, 2014 at 22:37

3 Answers 3

1

Use a combination of val() calls instead of append like so: $('input').val($('input').val()+cntrl). See here: http://jsfiddle.net/7fjgvuhh/

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

3 Comments

You don't need to call the same method twice, once inside the other, just use the anonymous function: $(input).val(function(i,v) { return v + cntrl; });
That's interesting, I've never seen that used before - thanks!
A (somewhat-belated) illustration.
1

Since you just want to show which button was pressed, you could give your buttons a value attribute (value="c", value="a" and value="t") in the HTML and use it in the JavaScript code to add it to the textbox.

Demo on Fiddle.

HTML:

<input id="one" type="text">
<input id="two" type="text">
<input id="three" type="text">
<div>
    <button id="c" class="buttons" value="c">c</button>
    <button id="a" class="buttons" value="a">a</button>
    <button id="t" class="buttons" value="t">t</button>
</div>

JavaScript:

$(document).ready(function () {
    $('#c').click(function () {
        $('#one').val($(this).val());
    })

    $('#a').click(function () {
        $('#two').val($(this).val());
    })

    $('#t').click(function () {
        $('#three').val($(this).val());
    })
});

Comments

0

To get the text of the button you should use the $.text() function

Here is the correct code

HTML


<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<div> 
<button class="buttons">c</button>
<button class="buttons">a</button>
<button class="buttons">t</button>
</div>

JS


$(document).ready(function () {

    $('input').click(function(){
       $(this).addClass('active').siblings('.active').removeClass('active')
    });

    $(".buttons").click(function () {
       var cntrl = $(this).text();
       var existing  = $('input.active').val();
       $('input.active').val(existing + cntrl);
    });
});

Here's a fiddle : http://jsfiddle.net/rua4cn8o/1/

4 Comments

This doesn't really do what's expected - he wants to append items to each input, but your code does a replace. Also, this doesn't work because the JQuery selector you're using won't match the inputs he provided
Thanks @Ben, I've updated my code. And i think he is trying to allow all the inputs to have multiple characters. That's why I think he's assigning class "active" to the most recently clicked input, and then appending the character to it.
And <input> elements are void/empty elements; in HTML they don't need to be closed (<input>) and in XHTML they're self-closing (<input />). Also what's $.text(), did you just mean the text() method? There is no jQuery.text() method.
@DavidThomas Thank you, I didn't know that. I will update my code accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.