1

I want to be able to have the user list things in an input box (comes up via an alert) and then once submitted have those options replace the default text in the string. They need to be separated after each comma as well and with each thing listed, the loop should paste the text into it when it runs, and repeat with the rest in the list.

Is this possible?

This is what I'm working on right now: http://jsbin.com/nivah/1 and it'll only allow you to type in one thing. Because if you type in several options with commas it includes the commas and all the options in the link which obviously won't return anything.

Here's my javascript code.

var link0='http://www.twitter.com/';
var user=['bob', 'joe', 'tony'];
var user = prompt("I want to message:", user);

var link3=".tumblr.com";
var iframe = document.getElementById("username");
    var opt = (link0 + user + link3);
    var el = document.createElement("iframe");
for(var i = 0; i < user.length; i++) {
    el.setAttribute("src", opt); 
    iframe.appendChild(el);
}
var array = user.split(',');

Anyone know what I am doing wrong?

I had it working before to repeat the loop based on what i submitted but it was doing it by make each letter of the word its own link in the iframes. Which is not what I want, but as close as I've ever gotten to the loop interaction with the user inputted text.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
taliciaem
  • 17
  • 1
  • 5

1 Answers1

2

You need to use the split() but at the right place in your code. Also, your logic is not going to work, You need to create each iframe within the for loop, something like this -

WORKING DEMO - http://jsbin.com/ciquvatu/1/edit

var link0='http://www.tumblr.com/ask_form/';
var user = ['killthemwithyourawesome', 'barrowman-ilove', 'down-with-these-ships'];
var user = prompt("I want to message:", user);

var link3=".tumblr.com";
var array = user.split(',');

for(var i = 0; i < array.length; i++) {
    var iframe = document.getElementById("username");
    var opt = (link0 + array[i] + link3);
    var el = document.createElement("iframe");
    el.setAttribute("src", opt); 
    iframe.appendChild(el);
}

This should do what you are looking for.

Nitish Dhar
  • 2,282
  • 15
  • 18
  • OMG! Thank you so much. I had a feeling I had things totally scrambled. It was driving me crazy! Again - Thank you! – taliciaem May 29 '14 at 11:42
  • you are welcome :) but you should always assess your code eg- you knew you want to split the user input & then create the iframes, then why will split happen after creating the iframes. – Nitish Dhar May 29 '14 at 12:22
  • That does make sense. Doh! I'm a bit of a dunce most days. That and I'm not too familiar with Javascript yet. Still learning. :D Thanks muchly for the tip though. :) Hopefully I remember that. – taliciaem May 29 '14 at 13:25