Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to figure out how to make a text box number reflect the number on the array i am trying to show. My code in attached. I am trying to make it that if i type "4" then 4 of my skills would pop up on my site.

    <script type="text/javascript">
function myduties1() {
var a= document.getElementById("DNumber1").value;
var duties1= ["Building Houses","Using Heavy Machinery","Lifting","Fixing","Working Fast"];
var b= a-1
var c=""
while (a>=b) {
c== "duties1[b]";
b++;
}

document.getElementById("mduties1").innerHTML =c;
}

</script>
<p><button onclick="myduties1()">Click Here</button>to see my top <input type="text"       id="DNumber1" value=""> job duties</p>
    <p id="mduties1"></p>
share|improve this question
    
1) Syntax error. Missing semicolons in several places. 2) The initial value b is always a=1. So, the loop will always be iterated just once. 3) Nothing is assigned to C. Rather, the value of C is compared to the nth element of the array. Not to mention lack of error checking. Also, most web browsers have a javascript debugger that will help you identify errors, and write your script. –  Sam Varshavchik 42 mins ago

2 Answers 2

Possible solution:

function myduties1() {
    var a= document.getElementById("DNumber1").value;
    var duties1= ["Building Houses","Using Heavy Machinery","Lifting","Fixing","Working Fast"];
    var out = "";
    if (a <= duties1.length){
        var out = (duties1.slice(0,a)).join();
    }
    document.getElementById("mduties1").innerHTML =out;
}

Check this link jsfiddle to see a working example.

Hope it's useful!

share|improve this answer
function myduties1() {
  var a = parseInt(document.getElementById("DNumber1").value);
  var duties1 = ["Building Houses","Using Heavy Machinery","Lifting","Fixing","Working Fast"];
  var c=[];

  if (!isNaN(a)) {
    for (var iter = 0; iter < a; iter++) {
      c[c.length] = duties1[b];
    }
  } 

  // or:
  if (!isNaN(a))
      c = duties1.slice(0, a - 1);

  document.getElementById("mduties1").innerHTML = c.join("<br/>");
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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