0
Hello friends i would l like to know how you can make an alert that contain only letter "t" from <p>student</p>
Web
8 Answers
+ 2
Add an id to p.
const el = document.getElementById(id);
const text = el.textContent;
alert(text[text.indexOf('t')])
+ 1
Yes thank you very much
0
You can select p using querySelector("p"), this will work if you have only one paragraph on your page, if you have many, you can add id to paragraph and select that id.
Then you can use elem.innerText() to access value "student" from paragraph, where "elem" is element you selected.
Then you can find "t" but student have 2 "t" so i dont know what "t" you will look for, you can loop trought string in this case "student" and look for letter "t", than if you find assign it to variable
You can use alert() to output this value
0
More ways of selecting elements in javascript:
https://www.tutorialrepublic.com/javascript-tutorial/javascript-dom-selectors.php
0
Help me with example
0
I just counted letter "t" of word student, you can replace this word with any another and see how counter change.
I do this because i dont understand what you exualy try to do
You have comments what explain every line of code.
I used js6 syntax, but you can replace it.
https://code.sololearn.com/WUHP5b2VtqeE/?ref=app
0
wow it works perfect
Then how to make an alert for only letter 'u' from word 'student'
0
Check my code again, i set code to alert any letter "u" in student word. Try adding more letter "u" and you will see more then 1 alert, it will alert every letter now.
https://code.sololearn.com/WUHP5b2VtqeE/?ref=app