Forum Moderators: open

Message Too Old, No Replies

Change combo box into text field

         

hvanvelzen

2:27 am on Jun 21, 2005 (gmt 0)

10+ Year Member



Hi,

I was wondering if there is a way to change a combo box into a text field after an onclick event.
Thanks in advance!

<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
function changeToDropdown(CID) {
// How on earth?
}
</script>
</head>

<body>
<select name="persons" id="persons" style="width: 40px;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<a href="javascript:changeToDropdown('persons')">more then 10</a>
</body>
</html>

Moby_Dim

7:23 am on Jun 21, 2005 (gmt 0)

10+ Year Member



This is possible but may mean a lot of headache. You can change innerHTML of a <div>, for example.

Sathallrin

12:33 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
function changeToDropdown(CID) {
document.getElementByID(CID).innerHTML = '<input type="text" name="persons" id="persons" style="width:40px;">'
}
</script>
</head>

<body>
<div id="personbox">
<select name="persons" id="persons" style="width: 40px;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<a href="javascript:changeToDropdown('personbox')">more then 10</a>
</body>
</html>

hvanvelzen

12:42 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



"You can change innerHTML of a <div>, for example."

Using innerHTML I think causes problems with browsers like Firefox and Safari(?).

I tried using document.getElementById("persons).tagName.toLowerCase() == 'input' but no dice.

Wow, you would think that this would be a rather simple ...

Moby_Dim

12:53 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



yeah, Sathallrin, but may be better not to place in href attr , but: <a href="#" onclick="changeToDropdown('personbox')">more then 10</a>

hvanvelzen, no problems with FF in my practice (do not know about Safari personally).

When I've spoken about "headache", I meant retaining variable(s) values, etc. I suppose you want to change the type not only to play with this, but to achieve something valuable.

Moby_Dim

12:59 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



>> document.getElementById("persons).tagName.toLowerCase()...

the wrong way maybe. tagName is only a dom object attribute I suppose, but the object itself is something more. And probably - read-only one...

hvanvelzen

1:01 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Sathallrin, Moby_Dim you guys rock!

I really thought that the innerHTML property was buggy when it comes down to browser compatibility. But I just tested it in IE, Firefox (windows and mac) and Safari and it works just fine.

Thanks a bunch!

hvanvelzen

1:10 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



"yeah, Sathallrin, but may be better not to place in href attr , but: <a href="#" onclick="changeToDropdown('personbox')">more then 10</a>"

You're absolutely right. The funny thing is I actually always use the onlick event to trigger JavaScript. However when I'm working in a WYSIWYG editor (like Dreamweaver) I find it easier to type "javascript: ...." in the "Link" input box. So in this case it's just me being lazy!

But thanks for the advice!

Moby_Dim

1:19 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



and if i were u, i woun't use <a ...> at all in this case. Use simple <span... onclick=....>..., image, or else.

hvanvelzen

1:43 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



"and if i were u, i woun't use <a ...> at all in this case. Use simple <span... onclick=....>..., image, or else."

You mean something like this:
<span onclick="alert('hi!');" style="cursor: hand;">Click me</span>

Dont't know about that one Moby_Dim ...
Here's the thing:
Using <span> I think deprives me of showing the user pseudo text styles (link, visited, active, hover). I don't know how to simulate these properties without using the <a> tag.
And in Firefox you don't get to see the hand cursor if you don't use the anchor tag.

Or am I missing something?

Moby_Dim

1:52 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



You may use cursor : pointer , for example. To simulate properties, e.g. hover or focus you may use javascript function (event handlers) to change the text color, etc.

But, each way has it's own ++ and -- ,of course.

j4mes

2:01 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Here's the code I use (works in FF and IE):

(script in the <head>)


function other(event) {
if (event.selectedIndex == event.options.length - 1) {
document.getElementById("other").style.display = "";
}
else {
document.getElementById("other").style.display = "none";
}
}

Along with these in the body:


<select name="event" onchange="javascript: other(this);">
<option>1</option>
<option>2</option>

... your options ...

<option>More than 10</option>
</select>

<div id="other" style="display: none;">
<input type="text" class="text" name="other-event" />
</div>

When the last option is selected, the input box appears, allowing the user to type in whatever.

HTH, J.

Edit: fixed code :)

hvanvelzen

2:54 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Hi J4mes,

I like your idea also. The beauty of it is that you offer more flexibility to your users. If someone "accidently" clicks on "More than 10" they still are able to choose between typing and selecting.
I like, I like!

Thanks everybody!
If you have more ideas I'll be glad to hear them!

Moby_Dim

3:06 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



does anybody here have sticky mail problems?

j4mes

3:10 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



does anybody here have sticky mail problems?

Just stickied you so I guess not, have you tried contacting Brett?

J.