2

I have a plugin that I am trying to grab the the number of the current slide which will then be written to a input box with the ID of "input1" each time the slide is changed. Does my syntax make sense?

function currentSlide(){
    var current = $('#slider').data('AnythingSlider').currentPage; 
    document.getElementById("input1").innerHTML=current;
};
1
  • You could replace document.getElementById("input1").innerHTML=current; by $("#input1").html(current); besides that it's hard to tell without the html source... Feb 10, 2012 at 16:30

3 Answers 3

3

If input1 is an input (as the name implies) use:

document.getElementById("input1").value = current;

Or to use jQuery only:

function currentSlide(){
    var current = $('#slider').data('AnythingSlider').currentPage; 
    $("#input1").val(current);
};
2

If you're trying to change what is shown in an <input> elemnt, set the .value, NOT the .innerHTML.

Think about it. Do you write <input>Blah blah blah</input>, or do you write <input value="Blah blah blah" />? JavaScript treats elements the same way HTML does.

0
function currentSlide(){
  var current = $('#slider').data('AnythingSlider').currentPage; 
  $('input1').val(current);
};

If you're using jQuery, I recommend sticking with jQuery selectors throughout your code. I've updated line 3 to use jQuery to select input1. Instead of using innerHTML, use jQuery's val function which will set input1's value to the currentstring.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.