0

I want to add images, but when I add them, the text box moves and is not completely in the gray area (the div). I want the gray area to expand by a certain amount when an image is uploaded. I am not sure, but I think that Javascript would work and help solve this problem. I also was wondering how to bold specific things, as right now my code bolds everything. For example, if a user highlighted a word and the selected bold, then the specific word would bold. Right now, it is bolding/italicizing/underlining everything. Thanks!

let cssProps = {
  'font-weight': '',
  'font-style': '',
  'text-decoration': ''
}

$('input[name="textStyle"]').change(function() {
  const val = $(this).val()
  if ($(this).is(':checked')) {
    switch (val) {
      case 'bold':
        cssProps['font-weight'] = 'bold';
        break;
      case 'italic':
        cssProps['font-style'] = 'italic';
        break;
      case 'underline':
        cssProps['text-decoration'] = 'underline';
        break;
    }
  } else {
    switch (val) {
      case 'bold':
        cssProps['font-weight'] = '';
        break;
      case 'italic':
        cssProps['font-style'] = '';
        break;
      case 'underline':
        cssProps['text-decoration'] = '';
        break;

    }
  }
  $('textarea[name="styledText"]').css(cssProps)
});
var loadFile = function(event) {
   var image = document.getElementById('output');
   image.src = URL.createObjectURL(event.target.files[0]);
};
    body {
  background-color: #5f5959;
  color: #000000;
}

textarea[type=text],
select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

textarea[type=submit] {
  width: 100%;
  background-color: #f9f9f9;
  color: 000000;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

textarea[type=submit]:hover {
  background-color: #908989;
}

div {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
  display: inline-block;
}
.button {
  background-color: #393737;
  border-radius: 4px;
  color: #f9f9f9;
  border-style: solid;
  border-width: 2px;
  border-color: #393737;
  width: 130px;
  padding: 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: auto;
  cursor: pointer;
  font-family: 'Poppins', sans-serif;
}
#file {
  background-color: #393737;
  border-radius: 4px;
  color: #f9f9f9;
  border-style: solid;
  border-width: 2px;
  border-color: #393737;
  width: 130px;
  padding: 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: auto;
  cursor: pointer;
  font-family: 'Poppins', sans-serif;
  margin-bottom: 10px;
}
input[type="file"] {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="margin-left: 20px;">
    <p><input type="file"  accept="image/*" name="image" id="file"  onchange="loadFile(event)" style="display: none;"></p>
    <p><label id="file" for="file" style="cursor: pointer; margin-bottom: 2px;">Upload Title Image</label></p>

    <script>

    </script>
    <form style="font-family: 'Poppins', sans-serif; color: #000000 padding-left: 100px;">
      Bold:<input name="textStyle" type="checkbox" value="bold" />
      <br> Italic:
      <input name="textStyle" type="checkbox" value="italic" />
      <br> Underline:
      <input name="textStyle" type="checkbox" value="underline" />
    </form>
</div>

<div style="margin-left: 55px; width: 1000px; height: 680px;">
  <p><center style="font-family: 'Poppins', sans-serif; font-size: 13px;">When submit, your text will clear and your blog will be published!</center></p>
  <textarea type="text" placeholder="Title" style="font-family: 'Poppins', sans-serif;"></textarea>
  <form style="font-family: 'Poppins', sans-serif;">
    <img id="output" width="200"/><textarea name="styledText" type="text" style="font-family: 'Poppins', sans-serif; border-width: 2px; border-style: solid; outline: none; border: none; height: 445px;"></textarea>
    <input style="height: 50px; width: 1000px; font-family: 'Poppins', sans-serif; border-radius: 7px;" value="Submit" type="submit">
  </form>
</div>
  • You are asking two specific questions, issue with image download and another about how to format specific text as bold, that are not related to each other. I suggest you edit your question and pin-point one issue, then open another question that pin points your other unique issue. – dale landry Apr 07 '21 at 02:50
  • I added 2 specific questions because SO would not let me post my question without extra details. Please fix this, an SO admin already noted me for a similar issue. Thanks! I will add a separate question. – Pickl Pilot Apr 07 '21 at 02:52
  • Have a look at the following SO article on selecting highlighted text. It may help with your bold text issue. https://stackoverflow.com/questions/60359921/how-to-get-highlighted-text-position-from-textarea – dale landry Apr 07 '21 at 02:55
  • In addition, you can't actually format words within a textarea separately - it's all or nothing when it comes tto styling textarea content. Try pursuing the HTML attribute [contenteditable](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable) if you wish to continue with styling selected text ( a non trivial exercise). – traktor Apr 07 '21 at 03:08
  • Also answers may well depend of the blog software or type of site you are using - for example, wordpress answers won't apply to a custom node server. – traktor Apr 07 '21 at 03:19

0 Answers0