Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

javascript

in javascript, I want to find a way like below.

If value = 1 then
placeholder = dropdown
else
placeholder = textbox
end if

Is it possible in Javasript or jquery?

<placeholder></placeholder>
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

The answer is simple: Yes, it is possible.

But it depends on what you wanna do.
If the "value" is coming from a user input checkbox then you have to disable and hide the one control and enable and show the other control.
On Submit the disabled form controls are not submitted.

So what is your scenario for this requirement?
Avatar of ITsolutionWizard

ASKER

the value came from the <select></select>

The <placeholder> should be generic. It is not necessary to do disabled or not disabled.
just like what i mentioned. value 1 show drop down value 2 show text box.

Is that clear ?

Thanks
That is clear but my question is: are you sure that you want to loose the text input or the dropdown selection when the user switches the selector value?

Therefor the most developer decide to have both controls on the form and enable the requested one as selected.

The "dynamic" approach has NO advantages, only difficulties. For example: where do you get the dropdown options when you have 50 states for selection?
This is an example that works but that I would not recommend:
<html>
<head>
<title>Zvonko &#42;</title>
<script>
function switchOption(theSel){
  $("inputArea").innerHTML = $("inputTemplate"+theSel.value).innerHTML;
}

function $(theId){
  return document.getElementById(theId);
}
</script>
</head>
<body>
<form>
<select name="userSel" onChange="switchOption(this)">
<option value="1">Text</option>
<option value="2">DropdownText</option>
</select>
<hr>
<span id="inputArea">
<input type="text" name="usrInput" >
</span>
<hr>
<input type="submit">
</form>
<form name="hiddenForm" style="display:none;">
<span id="inputTemplate1">
<input type="text" name="userInput" >
</span>
<span id="inputTemplate2">
<select name="userInput">
<option value="1">One</option>
<option value="2">Two</option>
<option value="2">Three</option>
</select>
</span>
</form>
</body>
</html>

Open in new window


I would prefer disabling and enabling hidden elements on same form area.
no. this is not what I am asking for. I am asking for a <place holder></place holder> in html so it is just like container. It can accept text box, drop down, and etc. based on the value it is provided.

Your code have static html text and select.
the placeholder needs the final code for the input or for the select.
where should that select field definition come from?
???? ITsolutionWizard, I have read through this, and I am unable to see or get what you may mean by your using the -
"  a <place holder></place holder> in html  so it is just like container."

I do not believe that this will be an effective way (if you can do that), ?
What I would do is have an existing HTML element like <span> or <div>, , AS A CONTAINER that you dynamically load the "contents" into as a <select> or <textbox>,  if you have an empty
     <span id="dynamic"></span>
it will disappear, and in JS you can load something into the HTML for it -

document.getElementById("dynamic").innerHTML = '<select name="d">
<option value="a">1</option>
<option value="b">2</option>
</select>';
and it will show the <select> or anything you place in it correctly as DHTML

Although you possibly  can put some <place holder> in it to the HTML document, you would then have to find it and remove it, then you can get use the DOM to "create" a <select> and add options, then you would need to use an insert command to place that element,  hopefully, in the same place of the Document structure, where you removed the place holder, not so easy.
@Zvonko, more than likely, but I hoped to separate a Place Holder from the change of HTML that most JS uses, did not think that ITsolutionWizard got that, ,  or was used to templates or other programming that uses place holders.
Guys, I know it may be not very easy. In the same case, I can do that in asp.net . Since my project is in html5 , that why I asked this question.

If you don't have solution or too hard for you. It is ok. But I have to keep the question as it.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ ITsolutionWizard: you are right, its too hard for me <|;-)