Zerivo Posted October 15, 2018 Share Posted October 15, 2018 Hi, i make one but it will be many line same, like this with 20. document.getElementById("Test1").value = Dotoff; document.getElementById("Test2").value = Dotoff; document.getElementById("Test3").value = Dotoff; i wonder can it make all id in one? i tried it: document.getElementById("Test1","Test2","Test3").value = Dotoff; but it didn't work well, only Test1 change. some know how make all 20 in one? it can save me lots space/line ;) Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 Use single class name for all and use getElementsByClassName instead. Link to comment Share on other sites More sharing options...
Zerivo Posted October 15, 2018 Author Share Posted October 15, 2018 can you show me example? btw Dotoff is text inside, i use this code var Dotoff = String.fromCharCode(9678); which look like: ◎ Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 Same priciple https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_element_getelementsbyclassname_loop Link to comment Share on other sites More sharing options...
JMRKER Posted October 15, 2018 Share Posted October 15, 2018 'dsonesuk's idea more dynamic with code changes, but you could also do it old style in a pinch. <script> for (var i=1; i<=20; i++) { document.getElementById("Test"+i).value = Dotoff; } </script> Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 IF the number of inputs with that id remain the same, else you would have to change for loop number every time. Link to comment Share on other sites More sharing options...
JMRKER Posted October 15, 2018 Share Posted October 15, 2018 Yes, hence the reason for my comment: " 'dsonesuk's idea more dynamic with code changes ..." Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2018 Share Posted October 15, 2018 Hence I explained specifically what that would mean. Link to comment Share on other sites More sharing options...
iwato Posted October 16, 2018 Share Posted October 16, 2018 Then too, you could use jQuery, it will do the counting for you. document.getElementById("Test1").value = Dotoff; document.getElementById("Test2").value = Dotoff; document.getElementById("Test3").value = Dotoff; <element1 class='test'></element1> <element2 class='test'></element2> <element3 class='test'></element3> $('.test').val('Dotoff'); Roddy Link to comment Share on other sites More sharing options...
smus Posted October 18, 2018 Share Posted October 18, 2018 same thing in javascript: var test = document.getElementsByClassName("test"); for (var i=0;i<test.length;i++){ test[i].value = Dotoff; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now