0

I want to remove the "http" if it is put in the url part of the input link, before the data is sent.

this my input code look onclick=

<input style="    outline: none;" type="button" onclick="formatText ('link:url');" class="btn btn-yeni" value="link"/>

This my javascript code (the received data is sent to another file and replaced.)

<script type="text/javascript">
function formatText(tag) {
   var Field = document.getElementById('entry_girdi');
   var val = Field.value;
   var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
   var before_txt = val.substring(0, Field.selectionStart);
   var after_txt = val.substring(Field.selectionEnd, val.length);
	Field.value += '(' + tag + ')';
}
</script>

what i want to do If the input value is "link: http: //example.com" I would like to change it and post it as "link: example.com".

2 Answers 2

4

Can you try in your url string :

var result = url.replace(/(\w+:|^)\/\//, '');

result variable will hold "link : example.com" in place of "link : http://example.com"

Sign up to request clarification or add additional context in comments.

13 Comments

Onclick does not work when I write this between the codes. the operation of the system is as follows; OnClick is sending data to the textarea field. then this data is posted with submit. I want to remove the http tag in onclick before posting. thanks, but the code you gave did not work.
is document.getElementById('entry_girdi') the element that hold the url value ?
yes but onclick does not work when I write your code between JavaScript codes at the top.
if you replace var val = Field.value; line with this line var val = Field.value.replace(/(\w+:|^)\/\//, '') what happens ? <br> and you'll need only this part if you want only to remove the "http://" string from url then you can return it as return val + '(' + tag + ')'
for url i mean the Field.value which hold the url that you want to format. sorry for not being clear :). also this regex will help you even if the url has "https://" ;)
|
1

Use the replace() function to replace part of a string.

function formatText(tag) {
  var Field = document.getElementById('entry_girdi');
  Field.value = Field.value.replace("http://", "");
  Field.value += '(' + tag + ')';
}

3 Comments

He'll have to remove https too eventually
Why did you delete the lines I selected? I am sending data to another file using these lines. var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd); var before_txt = val.substring(0, Field.selectionStart); var after_txt = val.substring(Field.selectionEnd, val.length);
The code never used any of those variables, and they didn't seem relevant to the question. You can add them back in your complete application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.