0

I have product name(in paragraph), text form and button. The products have id's as follows p1,p2... The input types have id's as follows i1,i2...After typing something in the form and clicking submit I want this to change the default product text. I have the following function which works only for one set(paragraph,form and button). The problem is that this product function works only for p1,i1 I want it to fork for p1,i1,p2,i2 and etc.

 function product(id){
            var userInput = document.getElementById("i1").value;
            document.getElementById("p1").innerHTML = userInput;
        }

The function call is as follows:

<button type='button' onclick='product()'>Name product</button>
1
  • You can use a very little jQuery for this. Commented Jun 22, 2011 at 6:38

2 Answers 2

3

what you need is to pass the index to the function:

function product(id) {
    var userInput = document.getElementById("i"+id).value;
    document.getElementById("p"+id).innerHTML = userInput;
}

your html would look like:

<button type='button' onclick='product(1)'>Name product</button>

hope this helps.

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

Comments

1

you can use a loop to set make all the changes

function product(){
   for (var i=0;i < totalNumberofItems;i++){
      var userInput = document.getElementById("i"+i).value;
      document.getElementById("p"+i).innerHTML = userInput;
   }
}

That is if you want the button to update all the fields

Comments

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.