views:

35

answers:

2

How can I change my textarea background image when I select a value from the select menu?

<html>
<head>
<title>  Your Title  </title>

<script>
function kool()
{
`enter code here`abc.style.background="img1.bmp"
}
</script>
</head>
<body>

<textarea name="abc">
1) Make paper airplanes out of the exam. Aim them at the instructor's left nostril. 
2) Bring cheerleaders during an exam.
</textarea>

<select id="xyz" onchange="kool()">
<option value="A">Image 1</option>
<option value="B">Image 2</option>
</select>

</body>
</html>
+2  A: 

This should work:

<textarea id="text">
Lorem ipsum dolor sit amet, ...
</textarea>

<select onchange="document.getElementById('text').style.backgroundImage = 'url(' + this.value + ')';">
<option value="image1.png">Image 1</option>
<option value="image2.png">Image 2</option>
</select>
Douwe Maan
in this where do i specify path to my image ?? <select onchange="document.getElementById('text').style.backgroundImage = 'url('img1.bmp + this.value + ')';">
subanki
In the `value=""` for the `<option />`!
Douwe Maan
wow Douwe u r awsome, thanx friend , it works
subanki
Happy to help, but if you would +1 and accept my answer I'd be even happier ;)
Douwe Maan
in your code how do i trace my textarea, i mean my textarea is actually in a iframe
subanki
+1 for getting it right :)
Sarfraz
i gave +1 too, how to set my textarea path
subanki
@sAc Thanks ;)@subanki How do you mean 'set my textarea path' and 'trace my textarea'? You can get your textarea in JavaScript using `document.getElementById('text')`.
Douwe Maan
my textarea is in a <iframe name= "dork">
subanki
Your textarea is INSIDE the `iframe`? Try this: `window.frames['dork'].getElementById('id').style.etc`, replace `id` with the actual `id` attribute of the `textarea` inside the `iframe`.
Douwe Maan
dont worry i figured it out already , thanx buddy i am adding ur name also in my credit page, thanks again
subanki
Haha, okay, thanks ;)
Douwe Maan
A: 

It should be:

<script>
function kool(input)
{
   var el = document.getElementById('abc');
   el.style.backgroundImage = 'url(' + input + ')';
}
</script>
</head>
<body>

<textarea name="abc" id="abc">
1) Make paper airplanes out of the exam. Aim them at the instructor's left nostril. 
2) Bring cheerleaders during an exam.
</textarea>

<select id="xyz" onchange="kool(this.value)">
<option value="A.jpg">Image 1</option>
<option value="B.jpg">Image 2</option>
</select>

Make sure that paths are correct.

Sarfraz
`url()` isn't a JavaScript function, so that won't work ;)(Edited it for ya)
Douwe Maan
@Douwe M.: Thanks buddy, i forgot those quotes :)
Sarfraz