Change the placeholder when the select option is changed using jQuery

4.8k views Asked by At

I have a site with the same form in different spots on the page. I would like to be able to have a placeholder in a text input change when I change the select option. I have a JSFiddle with a kinda example: http://jsfiddle.net/y1jhhqLz/

All the forms look like this:

<form id="form1">
    <select id="mySelect1" onClick="changePlaceHolder('#mySelect1','#myInput1')">
        <option value='-1'>Form 1</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput1" type="text" placeholder="Form 1"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

And in this example, I would like to have the place holder change on the input with the id myInput, when I select the option Third.

I have tried many different ways to include the most recent attempt, which I put on the JSFiddle.

Any help would be greatly appreciated!

5

There are 5 answers

0
Daniel K. On BEST ANSWER

Set select onclick attribute to

onclick="changePlaceHolder(this,'#myInput1');"

and change javascript function to

function changePlaceHolder(dropdown, element) {
    if (jQuery(dropdown).val() == "Third") {
         jQuery(element).attr('placeholder', 'You did it correctly!');   
    } else {
         jQuery(element).at}tr('placeholder', '');   
    }
}

in JSFiddle, you should set the loading behaviour to "no wrap - in head"

enter image description here

Done!

1
depperm On

Add a change listener to the select, then get the value of the select and set the attribute placeholder to said value:

$('select').on('change',function(){
    var place=$(this).val();
  var num=$(this).attr('id').substring(8);
    $('#myInput'+num).attr('placeholder',place);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<p>How do I get it so that when I click on the option that says Third Choice (for any form), it will put in the text box (with the form and number ) a placeholder: "You did it correctly"? But it shouldn't change any other form but the one where you change the select to the Third Choice option. I tried using a onClick function.
</p>
<br><br>
    <h2>Please pick the third option from Form 1:</h2>
<form id="form1">
    <select id="mySelect1" onClick="changePlaceHolder('#mySelect1','#myInput1')">
        <option value='-1'>Form 1</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput1" type="text" placeholder="Form 1"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

<form id="form2">
    <select id="mySelect2">
        <option value='-2'>Form 2</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput2" type="text" placeholder="Form 2"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

<form id="form3">
    <select id="mySelect3">
        <option value='-3'>Form 3</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput3" type="text" placeholder="Form 3"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

0
KJ Price On

Several things going on. First, remove the onClick events (we are not looking for when the select is clicked but rather when it is changed) and instead use jQuery's on method. Now changePlaceHolder has access to this which is the actual select box which was changed.

See below:

jQuery('select').on('change input', changePlaceHolder);

function changePlaceHolder() {
    //I have to use jQuery instead of $ because it is on a wordpress site
    if (jQuery(this).val() == "Third") {
        jQuery(this).next().attr("placeholder","You did it correctly!");
    } else {
        jQuery(this).next().attr("placeholder"," ");
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <h2>Please pick the third option from Form 1:</h2>
<form id="form1">
    <select id="mySelect1" >
        <option value='-1'>Form 1</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput1" type="text" placeholder="Form 1"/>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"/>
</form>

Other things to note: your <input> tags to not require an ending tag </input>, instead write them like this <input />. Note that the "/" ends the tag. You want to edit the input immediately following your select, in which case you can use jQuery(this).next() which will select the element immediately following the current this element.

0
skobaljic On

For your specific case I would not touch source code, would define all functionality in script. Setting the onlick event inline is not a good idea, scripts should not go together with content.

I wrote this script having in mind that your select and input tags do not have to be siblings, the only condition is that both belong to same form:

/* This way you can use $ in your Wordpress */
(function ($) {
    $('select').on('change', function(e) {
        var thisSelect = $(this);
        var thisSelectValue = thisSelect.val();
        var thisInput = thisSelect.closest('form').find('input[id^=myInput]');
        if (thisSelectValue == 'Third') {
            thisInput.attr("placeholder", "You did it correctly!");
        } else {
            thisInput.attr("placeholder", "");
        }
    });
}(jQuery));
p {
    max-width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2>Please pick the third option from Form 1:</h2>
<form id="form1">
    <select id="mySelect1">
        <option value='-1'>Form 1</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput1" type="text" placeholder="Form 1"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

<form id="form2">
    <select id="mySelect2">
        <option value='-2'>Form 2</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput2" type="text" placeholder="Form 2"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

<form id="form3">
    <select id="mySelect3">
        <option value='-3'>Form 3</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput3" type="text" placeholder="Form 3"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

0
Hassan Ali Shahzad On

Please find the working code for you:

<!DOCTYPE html>
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
        
<script type="text/javascript">


   
function changePlaceHolder(element,target) {

    alert(element);
    
    
    if ( element == "Third") {
        jQuery(target).attr("placeholder","You did it correctly!");
    } else {
        jQuery(target).attr("placeholder","");
    }
    
}


</script>

<br><br>
    <h2>Please pick the third option from Form 1:</h2>
 
 <form id="form1">
    <select id="mySelect1" name="mySelect1" onclick="changePlaceHolder( this.value ,'#myInput1' );">
        <option value='-1'>Form 1</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput1" type="text" placeholder="Form 1"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>


<form id="form2">
    <select id="mySelect2">
        <option value='-2'>Form 2</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput2" type="text" placeholder="Form 2"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form>

<form id="form3">
    <select id="mySelect3">
        <option value='-3'>Form 3</option>
        <option value='First'>First Choice</option>
        <option value='Second'>Second Choice</option>
        <option value='Third'>Third Choice</option>
    </select> 
    <input id="myInput3" type="text" placeholder="Form 3"></input>
    <input class="dontChangeME" type="text" placeholder="Don't Change This One!"></input>
</form> 

</body>
</html>