/    Sign up×
Community /Pin to ProfileBookmark

Can’t figure out what’s wrong here

I’m writing a program for my web programming class where we need to type a word, the number of times we want to repeat the word, and the color we want the word to be. I’m having a couple of issues with it. First, I can’t figure out how to change the color based on what I type into the textbox. And second, I can’t get anything to display when I click the button. Here’s all the code I typed up so far.

[code]
<html>
<head>
<title></title>

<style type=”text/css”>

body {
background-color: #D3D3D3;
font-family: arial;
text-align: right;
color: #008B8B;
}

#contentwrap {
border: 8px #800000 solid;
padding: 20px;
width: 600px;
border-radius: 25px;
text-align: right;
background: white;
margin: 40px auto 0px auto;
}
#formwrap {

text-align: center;
margin: 0px 0px 60px 0px;
min-height: 300px;
}

#title {
font-size: 2.2em;
border-bottom: 7px #008B8B double;
padding: 10px 0px 10px 0px;
color: #008B8B;
text-align: center;
}

#formtext {
text-align: center;
margin-top: 5px;
}

.formfield {

text-align: center;
margin: 5px 20px 10px 20px;
}

#button {
border-radius: 20px;

}

#output {
font-size: 1em;
}

#repeatedword {
color: usercolor;
}

</style>

<script src=”https://code.jquery.com/jquery-3.4.1.min.js” integrity=”sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=” crossorigin=”anonymous”></script>
<script src=”https://code.jquery.com/ui/1.12.1/jquery-ui.min.js” integrity=”sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=” crossorigin=”anonymous”></script>

<script type=”text/javascript”>

$(document).ready(function(){

$(“#button”).on (“click”, function(){

var word = parseFloat(“#word”);
var repeatnum = parseFloat(“#repeatnum”);
var usercolor = parseFloat(“#usercolor”);
var msg = “”;

for(var i = 1;i <= repeatnum; i++){

msg += “<div id=’repeatedword’>” + word + “</div>”;

}

$(“#output”).html(msg);

});

}); // ends document.ready

</script>

</head>
<body>

<div id=”contentwrap”>

<div id=”title”>Fun with Loops</div> <br />

<div id=”formwrap”>

<form>

<div id=”formtext”>Enter any word</div>
<div><input type=”text” id=”word” class=”formfield” size=”20″></div>

<div id=”formtext”>Enter number of times to repeat word</div>
<div><input type=”text” id=”repeatnum” class=”formfield” size=”20″></div>

<div id=”formtext”>Enter color for text</div>
<div><input type=”text” id=”usercolor” class=”formfield” size=”20″></div>

<div style=”margin-top:10px;”>
<input type=”button” value=”Show Output” id=”button”>
</div>

</form>

<div id=”output”></div>
</div>
</div>
</body>
</html>
[/code]

to post a comment
CSSJavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rpg2009Mar 24.2021 — > @JIMBOYKELLY#1629583 var usercolor = parseFloat("#usercolor");

The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.

See [parseFloat - mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)

You are looping creating multiple divs with the same id name. id names should be unique. You could use a classname instead.

msg += "&lt;div class ='repeatedword'&gt;" + word + "&lt;/div&gt;";

For the colour one method might be to include an inline style. "style = 'color:" + userColor + ";'". Better than concatenating like this you could use a [template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
×

Success!

Help @JIMBOYKELLY spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...