I had this as part of an input tag:
onkeyup = "this.value = '#' + this.value;"
Which adds a hashtag with each letter. I only want 1 hashtag.
I also tried this:
onkeyup = "if (this.charAt(0) != '#') this.value = '#' + this.value;"
and this:
onkeyup = "if (this.charAt(0) != '#') this.value = '#' + this.value; return this.value"
I know ideally you want to put your scripts in a separate.js file, but this is just a really small project and I wanted to just throw it into the html code.
Here is an example
<input onkeyup="if (this.value[0].== '#') this.value = '#' + this.value"></input>
The problem was that you needed to do this.value.charAt(0)
or this.value[0]
not this.charAt(0)
Hopefully, this helps.
EDIT: As @epascarello suggested you could also use the onchange
event instead so that it does not move your cursor as you type.
<input onchange="if (this.value[0].== '#') this.value = '#' + this.value"></input>
Just get the final value and add it to before the first letter: something like:
let finalword = "#" + this.value.toString.trim();
What do you actually need to do with that?
I would just see if the string includes a #
and if not, use a ternary operator to concatenate like below:
return this.value.includes('#') ? true : '#' + value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.