0

I've simple javascript function mentioned below:

<script>
    function getValue(val)
    {
      document.getElementById('answer').value = val;
    }
</script>

This function get's called on click of span and returned value gets displayed in input text. I've three questions:

1] On click of span, if I want to append current value of varible 'val' with previous value; and return appended value to calling function. can you please suggest, how to achieve same without using jQuery?

2] There is one span which is suppose to work like Backspace. So click of span, i want to remove last character from document.getElementById('answer').value [This also without using jQuery]

3] There is one span which is suppose to work like Navigation keys [Right & Left]. So click of span [Let's say right], i want to move cursor position of the input text one character right and similarly for Left [This also without using jQuery]

Can you please suggest some pointers to achieve this?

Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
Pawan Mude
  • 1,609
  • 6
  • 19
  • 32
  • 1
    @HithamS.AlQadheeb Please help me in figuring out the connection between these two topics... –  Jan 01 '14 at 08:26

2 Answers2

1

For your question 1 I think you can do below. Code not tested

function getValue(val)
{
var currentVal = document.getElementById('answer').value
if(currentVal.length > 0 )
  currentVal = parseInt(currentVal);
      document.getElementById('answer').value = currentVal + val;
}

For question 2 :

Get the value and then do string operation to remove the last char. Its easy little google search for the string operations

For question 3 :

you can use event oncontextmenu for right click. Example below.

How can I capture the right-click event in JavaScript?

For moving cursor check below

Set keyboard caret position in html textbox

Community
  • 1
  • 1
A Paul
  • 8,113
  • 3
  • 31
  • 61
1
  • += oprator appends string to existing string(not applicable in this case).
  • use return keyword to return updated value.
  • for removing last character use substring.

so try:

function getValue(val)
{
  var currentText = document.getElementById('answer').value;
  var updatedText = currentText.substring(0,currentText.length-2) +  val;
  document.getElementById('answer').value = updatedText;
  return updatedText;
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110