1

Why this function is not working when i click on the Rate me button?

function incrementValue(){
    var value = parseInt($("#rateme").value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    $("#rateme").value = value;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <a onclick="incrementValue()">Rate me</a><input type="text" id="rateme" value="50" />
  </li>
</ul>

3
  • The function call is okay.The problem might me with the logic. jsfiddle.net/w056zv3L Commented Sep 8, 2017 at 21:02
  • Why would you use inline javascript event handling instead of using jQuery? Commented Sep 8, 2017 at 21:02
  • @ErikPhilips It start working if i change id to #number. what is the reason? Commented Sep 8, 2017 at 21:04

1 Answer 1

2

In jQuery, it's $("#rateme").val() not $("#rateme").value, that's for DOM elements not jQuery objects:

function incrementValue(){
    var value = parseInt($("#rateme").val(), 10);
    value = isNaN(value) ? 0 : value;
    value++;
    $("#rateme").val(value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li><a onclick="incrementValue()">Rate me</a><input type="text" id="rateme" value="50" /></li>

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

4 Comments

It start working if i change id to #number. what is the reason?
@Rohit it's not about the id, you can change to whatever you like as long as you use it correctly, the problem is that you're calling a dom method on a jquery object, which is not correct.
It works with $('#number').value = value?! Idon't think so. Try again.
@Rohit update your question with the current code that you claim works.

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.