Jump to content

Recommended Posts

Hi, I would like to know if it's possible to use javascript (specially on Anki; if you know what it is) to format a text line into a html table.

Basically, what I want is to copy the definition of an online dictionary (japanese in this case):

長/long, leader/ながい、おさ/チョウ

And have the javascript read the first "/", select what's behind it, put in a <th>, select what's between first and second /, put in a <td>, and so forth. I have the CSS set up already. All of this in real time, like, when the software tries to show the line above, the JS will format it to show the stylized table to me. 

Is this even possible? Does the w3s guide teach this kind of stuff at some point?

Link to post
Share on other sites

Sure.  A javascript function could easily handle that. 

EDIT:

the function could have a 

https://www.w3schools.com/jsref/jsref_match.asp

and a 

https://www.w3schools.com/jsref/jsref_match.asp

Edited by niche
Link to post
Share on other sites

Cool, I did it.

<!DOCTYPE html>
<html>
<style>
.tk { border-collapse: collapse; }
.tk th, td { border: 1px solid #ddd; padding: 6px }
.tk th { width: 56px; background-color: #f7f7f7; font-size: 24px; font-weight: normal; }
.tk td { width: 240px; text-align: left; font-size: 16px; }
</style>
<body onload="kanjiFormat()">

<p id="jisho">#長#long, leader#ながい、おさ#チョウ#</p>

<script>
var codeKstart = '<table align="center" class="tk"><tr><th rowspan="2">'
var codeMstart = '</th><td>'
var codeRstart = '</td></tr><tr><td>'
var codeComma = '、'
var codeRend = '</td></tr></table>'

function kanjiFormat() {
    var str = document.getElementById("jisho").innerHTML; 
    var txt = str.replace(/#/, codeKstart).replace(/#/, codeMstart).replace(/#/, codeRstart).replace(/#/, codeComma).replace(/#/, codeRend);
    document.getElementById("jisho").innerHTML = txt;
}
</script>

</body>
</html>

Now:

- How do I make the search for forward slashes (/)? I replaced them with # for the sake of testing. Nevermind, it will try to replace the forward slashes from html as well. Gotta replace them with another symbol when I paste it.

- Also, sometimes there one less category (e.g. /電/electricity/デン), in that case I wouldn't need the comma var. Where do I put an if in this function?

Edited by RandomNumberGen
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...