1

I have a variable "count" in javascript and a radio button that I want to depend on that variable. There is a button to generate more radio buttons, which is why I need their name attributes to differ.

My code:

var count = 1;
function newForm(){
...<input name=count type="radio" value="Website" />...
}

But it's just setting the name of each additional radio button to "count" rather than the number "count" represents.

Here's the whole code:

var count = 1;
function newForm(){
var newdiv = document.createElement('div');
newdiv.innerHTML = '<div class="line"></div><br><input type="text" name="Name" 
class="field" placeholder="Full Event Name" /><br><input type="text" name="Location"       
placeholder="Event Location" class="field" /><br> <input type="text" name="Date" 
placeholder="Event Date" class="field" /> <br> <input type="text" name="End" 
placeholder="Event End Date (If Applicable)" class="field" /> <br> <input type="text" 
name="Time" placeholder="Event Time" class="field" /> <br> <input type="text"     
name="Tags" 
placeholder="Relevant Tags" class="field" /> <br> The info is from: <input name=count 
type="radio" value="Tweet" checked="" />Tweet <input name=count type="radio"   
value="Website" 
/>Website <input name=count type="radio" value="Tweet and Website" /> Tweet and  
Website';
if(count < 10) {
    document.getElementById('formSpace').appendChild(newdiv);
    count++;
}

}

That newdiv.innerHTML string above is all on one line in the code, by the way.

5
  • 2
    Why don't you post the entire code so we can see what you're doing with that string, and show you how to concatenate the variable into the string. Commented May 22, 2013 at 21:42
  • more code please. you probably have something wrong with the way you are creating the radio button. a missing quote or double quote or something. Commented May 22, 2013 at 21:42
  • 3
    Please share the whole JS. You're passing count as a string and not as a variable. It should looks something like this: '<input name="radio' + count + '" type="radio" value="Websaite" />'; Commented May 22, 2013 at 21:43
  • Oh God, don't create large pieces of markup with strings like that ! Commented May 22, 2013 at 21:49
  • Why don't you accept any of those answers? They both seem to answer the question. Commented May 23, 2016 at 17:55

2 Answers 2

2

If you're trying to create an element, use createElement() :

var count = 1;

function newForm(){
     var input = document.createElement('input');

     input.name  = count;
     input.type  = 'radio';
     input.value = 'Website';
}
Sign up to request clarification or add additional context in comments.

Comments

1

in your long string of innerHTML you need to escape your "count" variable... otherwise it's just a string... i.e.

'<input name='+count+' type="radio" value="Tweet and Website" />';

That will make it work but as everyone else is mentioning - you really shouldn't embed long html strings like this.

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.