2

i have 2 html text input so if in first one i put this link https://example.com/E-LrapH4RtQ&list=PLy0zopi719kGIefv799akiQ7uh-8ntNXt

it will be https://example.com/E-LrapH4RtQ

so now i want in second html input value to be E-LrapH4RtQ

<script language="javascript" type="text/javascript">
    function removeytb(string) {
        return string.split('&')[0];
    }
</script>

<input type="text" name="ytburl" onblur="this.value=removeytb(this.value);" />
<input type="text" name="name1" value="" />

4 Answers 4

1

You can change the onblur from:

onblur="this.value=removeytb(this.value);"

to:

onblur="removeytb(this, 'name1');"

In order to get from https://example.com/E-LrapH4RtQ the last part you can use split and pop:

'https://example.com/E-LrapH4RtQ'.split('/').pop()

So:

function removeytb(ele, secondInputName) {
  // set the value as before but using the element
  ele.value =  ele.value.split('&')[0];
  
  // get the last part from the ele splitting the string
  document.getElementsByName(secondInputName)[0].value = ele.value.split('/').pop();
}
<input type="text" name="ytburl" onblur="removeytb(this, 'name1');">


<input type="text" name="name1" value="">

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

Comments

0

This should Work var link = www.example.com/idontknowthelink& some?stuff; var splitLink = link.split('&'); console.log(splitLink[0] + splitLink[1]); you have the all link stocked in array then you do just change the src of the link

1 Comment

i put link manyally on <input type="text" name="ytburl" so it need to gets from there
0

 function removeytb(string) {
     var newUrl = string.split('&')[0];
     var name = newUrl.substr(newUrl.lastIndexOf('/') + 1);
     document.getElementsByName("name1")[0].value = name
     return newUrl;
 }
 
<input type="text" name="ytburl" onblur="this.value=removeytb(this.value);" />
<input type="text" name="name1" value="" />

Once you get the url, do a substring of it like below and set it to the element instead of splitting into array.

name = url.substr(url.lastIndexOf('/') + 1);

Comments

0

Ok this is a quick fix for what you are looking for!

The Function:

function useInput(InputValue,anotherInput)
  {
   InputValue =  InputValue.split('&')[1];
   if(!InputValue){InputValue='';}; //To avoid showing undefined
   document.getElementById(anotherInput).value=InputValue;
  }

The HTML

<input id="myInput" onKeyUp="useInput(this.value,'anotherInput')" value=""/>
<input id="anotherInput" type="input" value="" />

As the user types the input from the first field is captured, processed and placed as the value of the other input field (on the fly)!

3 Comments

the problem was in the ID for the second input, will update in a min.
Ok it works now.. i guess i will go ahead and add in the string splitting method myself also.. in a lil bit.
This solution is now fully working! ...all issues fixed! =)

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.