admin管理员组文章数量:1289845
I am trying to make a script that when you type in a hex value and press submit itchanges the text color to the color inputted.
It seems the problem is the way i am calling the variable "userInput" inside the variable new html
Any Ideas?
<script type="text/javascript">
function changeText3(){
var userInput = document.getElementById('userInput').value;
var oldHTML = document.getElementById('para').innerHTML;
var newHTML = "<span style='color:userInput'>" + oldHTML + "</span>";
document.getElementById('para').innerHTML = newHTML;
}
</script>
<p id='para'>Wele to the site <b>dude</b> </p>
<input type='text' id='userInput' value='#' />
<input type='button' onclick='changeText3()' value='Change Text'/>
I am trying to make a script that when you type in a hex value and press submit itchanges the text color to the color inputted.
It seems the problem is the way i am calling the variable "userInput" inside the variable new html
Any Ideas?
<script type="text/javascript">
function changeText3(){
var userInput = document.getElementById('userInput').value;
var oldHTML = document.getElementById('para').innerHTML;
var newHTML = "<span style='color:userInput'>" + oldHTML + "</span>";
document.getElementById('para').innerHTML = newHTML;
}
</script>
<p id='para'>Wele to the site <b>dude</b> </p>
<input type='text' id='userInput' value='#' />
<input type='button' onclick='changeText3()' value='Change Text'/>
Share
Improve this question
asked Nov 12, 2009 at 13:37
DasaDasa
2971 gold badge7 silver badges23 bronze badges
3 Answers
Reset to default 8I would suggest to use the style reference instead of adding more and more spans:
document.getElementById('para').style.color = userInput;
It's just the one line that's causing the problem:
var newHTML = "<span style='color:" + userInput + "'>" + oldHTML + "</span>";
you need to "inject" the userInput variable into your newHTML variable. See below;
<script type="text/javascript">
function changeText3(){
var userInput = document.getElementById('userInput').value;
var oldHTML = document.getElementById('para').innerHTML;
var newHTML = "<span style='color:" + userInput + "'>" + oldHTML + "</span>";
document.getElementById('para').innerHTML = newHTML;
}
</script>
<p id='para'>Wele to the site <b>dude</b> </p>
<input type='text' id='userInput' value='#' />
<input type='button' onclick='changeText3()' value='Change Text'/>
本文标签: javascriptcalling a variable inside htmlStack Overflow
版权声明:本文标题:javascript - calling a variable inside html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414928a2377454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论