YUNG FIJI
YUNG FIJI

Reputation: 1

How to pull an ID from a link the User inputted

HTML:

<body>
 <input type="text" id="userINPUT" />
 <button onclick="updatev1()">Submit</button>
   <div id="video1">
    </div>
    <div id="video2">
  </div>
 </body>

The html has two divs, and an input text box,and of course, a submit button. What the user is supposed to do is enter a youtube link into the text box, and submit it.

JavaScript:

    var userIN1 = document.getElementById("userINPUT");
    var userIN2 = userIN1.value;
    var index = userIN2.substring(string.indexOf('=') -1);
    alert(index);

Now what I want the JavaScript to do is to grab the youtube link, and pull the ID from said link.

EX. The user inputs the link. 'http://www.youtube.com/watch?v=-K7lEFmFcKs', then the JavaScript would take the link and grab '-K7lEFmFcK' and store it in a variable for later use.

'userIN2' would be the variable that would store the user input value, and 'index' would take the whole ID coming after the '=' symbol of the link and store it.

I know this is considered a small task, but any help would be great. Thanks!

Oh, and I heard these things can be done A LOT easier with jQuery. Should I use jQuery instead?

Upvotes: 0

Views: 51

Answers (2)

ashley
ashley

Reputation: 2767

This collects everything after v=

http://jsfiddle.net/7aZqB/4/

function getID(str) {
    return str.substring(str.indexOf('=') -1).replace('v=', '');
}

Upvotes: 0

lucasnadalutti
lucasnadalutti

Reputation: 5948

Sure should. It'd be that simple:

$('button').click(function() {
    var userIN2 = $('#userINPUT').val(); 
    var index = userIN2.split('=');
    index = index[1];
});

Upvotes: 1

Related Questions