0

I want to make such a box (textarea or inputtextfield) with a variable text in there and the variable depends on javascipt variable.

For example:

var a=prompt('some text');
if (a==1) {
   link="www.google.com"
} else if (a==2) {
    link="www.facebook.com"
}

and I have a textarea or input text field and their value in the text box can change to be either "www.google.com" or "www.facebook.com"

1
  • <input type="text" id="tx" > I don't know how to set its value Commented Jun 25, 2014 at 11:43

2 Answers 2

2

You can achieve it using the following:

HTML

  <input type="text" id="textfield" />
  <textarea id="textarea"></textarea>

USING JS ONLY

 var a=prompt('some text');
 var link = '';
 if (a==1) {link="www.google.com";}
 else if (a==2) {link="www.facebook.com";}
 document.getElementById('textfield').value = link;
 document.getElementById('textarea').innerText = link;

OR BY USING jQuery

 var a=prompt('some text');
 var link = '';
 if (a==1) {link="www.google.com";}
 else if (a==2) {link="www.facebook.com";}
 $('#textfield').val(link);
 $('#textarea').text(link);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I've been searching for this since yesterday til I think that this website can help me so I asked here. Thank you so much :D
0

Consider that this is your input field

<input type="text" name="link" />

To set the value to the input field, use the following jQuery code:

var a = prompt("Enter a value :");
var str = "";
var field = $('input[name=link]');
if(a===1)
  str = "www.google.com");
if(a===2)
  str = "www.facebook.com");
$(field).html(str);

Comments

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.