1

I've been trying to randomize a number with a button, but every time I clicked the button, the number randomizes, but the text loses it's CSS style.

CSS

.numberStyle {
  padding: 10px 10px 10px 10px;
  color: blue;
}

.numberStyle span {
  font-size: 100px;
}

Html

<class id="number1" class="numberStyle"><span>1</span></class>
<input type="button" value="MATCH!" style="font-size:50px;"></input>

Javascript

function randomize() {
  no1 = Math.ceil(Math.random() * 3);
}

function print() {
  $("#number1").text(no1);
}

$().ready(function() {

  $(":input").click(function() {
    randomize()
    print()
    alert("Change")
  })

})

my JSFiddle link : https://jsfiddle.net/he4rtbr0ken/9jfud4nz/2/

3 Answers 3

1

You want to change the text within the span rather than #number1

function print() {
    $("span").text(no1);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work if there's more than one span on the page. The selector isn't specific enough.
The point of a span is the same as any other tag, which is to categorize. If they need more specificity they should consider to either add a class or use a different tag all together. This answer is the most direct with the given information.
0

You are targeting the parent of the <span> element and then changing its text which is essentially removing the span and replacing it with only the number. You can fix this by targeting the span or including the span in the text you are adding.

Comments

0

Thanks for the answer!

I've targeted <span> element and it works.

replaced print() function with the codes below.

function print() {
  $("#number1 > span").text(no1);
}

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.