admin管理员组文章数量:1022949
I am very new to HTML/JS so I appologize if this is a basic question... I tried to look this up on the web and was unable to find a solution.
I am using a JS code to create an HTML. I am trying to set a "value" attribute with a var containing spaces (string with spaces). when inspecting the value in chrome I can see that the string is not set correctly.
This is my JS code:
var templateArray = templateString.split("\t");
for (var i = 0; i < templateArray.length; i++) {
htmlTemplate.push("<option value="+templateArray[i]+">"+templateArray[i]+"</option>");
}
This is the templateArray:
templateArray[0] = template_member_information
templateArray[1] = template - member information
This is what I get when inspecting in chrome:
<option value="template" -="" member="" information="">template - member information</option>
<option value="template_member_information">template_member_information</option>
I am very new to HTML/JS so I appologize if this is a basic question... I tried to look this up on the web and was unable to find a solution.
I am using a JS code to create an HTML. I am trying to set a "value" attribute with a var containing spaces (string with spaces). when inspecting the value in chrome I can see that the string is not set correctly.
This is my JS code:
var templateArray = templateString.split("\t");
for (var i = 0; i < templateArray.length; i++) {
htmlTemplate.push("<option value="+templateArray[i]+">"+templateArray[i]+"</option>");
}
This is the templateArray:
templateArray[0] = template_member_information
templateArray[1] = template - member information
This is what I get when inspecting in chrome:
<option value="template" -="" member="" information="">template - member information</option>
<option value="template_member_information">template_member_information</option>
Share
Improve this question
edited Jan 13, 2013 at 11:56
Sam
16.4k6 gold badges57 silver badges89 bronze badges
asked Jan 13, 2013 at 11:50
bearbear
211 silver badge2 bronze badges
1
-
1
should
templateArray[1] = template - member information
betemplateArray[1] = "template - member information"
or what are you doing here? – Stefan Commented Jan 13, 2013 at 11:54
3 Answers
Reset to default 2Don't write raw HTML, instead store only the values, then create the DOM elements on the fly.
Assuming you have all the values inside array named values
have such code to populate a drop down list with items having those values: (and text equal to the value)
var oDDL = document.getElementById("MyDropDownList");
for (var i = 0; i < values.length; i++) {
var option = new Option();
option.value = option.text = values[i];
oDDL.appendChild(option);
}
This way you don't have to mess around with quotes and the code is more flexible.
Live test case.
Why don't you simply put your value between quotes ?
htmlTemplate.push("<option value=\""+templateArray[i]+"\">"+templateArray[i]+"</option>");
Note that this would still fail with values containing quotes, but there would be less failings.
You forgot the quotes:
var str = "<option value='"+templateArray[i]+"'>"+templateArray[i]+"</option>";
htmlTemplate.push(str);
I am very new to HTML/JS so I appologize if this is a basic question... I tried to look this up on the web and was unable to find a solution.
I am using a JS code to create an HTML. I am trying to set a "value" attribute with a var containing spaces (string with spaces). when inspecting the value in chrome I can see that the string is not set correctly.
This is my JS code:
var templateArray = templateString.split("\t");
for (var i = 0; i < templateArray.length; i++) {
htmlTemplate.push("<option value="+templateArray[i]+">"+templateArray[i]+"</option>");
}
This is the templateArray:
templateArray[0] = template_member_information
templateArray[1] = template - member information
This is what I get when inspecting in chrome:
<option value="template" -="" member="" information="">template - member information</option>
<option value="template_member_information">template_member_information</option>
I am very new to HTML/JS so I appologize if this is a basic question... I tried to look this up on the web and was unable to find a solution.
I am using a JS code to create an HTML. I am trying to set a "value" attribute with a var containing spaces (string with spaces). when inspecting the value in chrome I can see that the string is not set correctly.
This is my JS code:
var templateArray = templateString.split("\t");
for (var i = 0; i < templateArray.length; i++) {
htmlTemplate.push("<option value="+templateArray[i]+">"+templateArray[i]+"</option>");
}
This is the templateArray:
templateArray[0] = template_member_information
templateArray[1] = template - member information
This is what I get when inspecting in chrome:
<option value="template" -="" member="" information="">template - member information</option>
<option value="template_member_information">template_member_information</option>
Share
Improve this question
edited Jan 13, 2013 at 11:56
Sam
16.4k6 gold badges57 silver badges89 bronze badges
asked Jan 13, 2013 at 11:50
bearbear
211 silver badge2 bronze badges
1
-
1
should
templateArray[1] = template - member information
betemplateArray[1] = "template - member information"
or what are you doing here? – Stefan Commented Jan 13, 2013 at 11:54
3 Answers
Reset to default 2Don't write raw HTML, instead store only the values, then create the DOM elements on the fly.
Assuming you have all the values inside array named values
have such code to populate a drop down list with items having those values: (and text equal to the value)
var oDDL = document.getElementById("MyDropDownList");
for (var i = 0; i < values.length; i++) {
var option = new Option();
option.value = option.text = values[i];
oDDL.appendChild(option);
}
This way you don't have to mess around with quotes and the code is more flexible.
Live test case.
Why don't you simply put your value between quotes ?
htmlTemplate.push("<option value=\""+templateArray[i]+"\">"+templateArray[i]+"</option>");
Note that this would still fail with values containing quotes, but there would be less failings.
You forgot the quotes:
var str = "<option value='"+templateArray[i]+"'>"+templateArray[i]+"</option>";
htmlTemplate.push(str);
本文标签: javascriptsetting a quotvaluequot attribute in html with a variable containing spacesStack Overflow
版权声明:本文标题:javascript - setting a "value" attribute in html with a variable containing spaces - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745580166a2157261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论