tags:

views:

91

answers:

3

Hi all,

I'm trying to set the value of three different input text fields with an onclick function.

I have an image that has this code...

<img src="images/delete_row.png" width="25" onClick="clearRow(0);" />

And I have three input text fields that all have the id of "0".

When I click my image I want to set the value of all three fields to empty.

Can someone please help me write a function that can do this?

Thanks!

A: 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

<script>
$(document).ready(function() {
  $('#pic').click(function() {
    alert('Clicked on pic - resetting fields')
    $('.field').val('')
  })
}
</script>

<img id="pic" src="image.png">
<input class="field" value="1">
<input class="field" value="2">
<input class="field" value="4">
<input class="field" value="5">
<input class="field" value="6">
<input class="field" value="7">
<input class="field" value="8">
<input class="field" value="9">
<input class="field" value="10">
jspcal
May be because of the use of jQuery.
rahul
First, I didn't downvote. Second, I imagine the downvote was because you suggested adding a 56K (un-gzipped) JavaScript library in response to how to clear three fields with JavaScript. I *love* jQuery, but sometimes straight JS will get the job done w/o the extra weight.
Doug Neiner
@Doug Neiner: jquery would be downloaded *once* (then cached for *an entire year*). you're missing the obvious benefit that using jquery will *save the OP time* when he needs to perform similar functions. (unless you're assuming this is the only javascript he will ever need). jquery is therefore a win, low-level js is less portable, more verbose, more time consuming, etc. your comment assumes a very short-term view (saving a *few insignificant bytes*, but costing the OP by not using a great all-purpose tool for similar reqs.)
jspcal
Doug Neiner
@Doug Neiner: no, not rewrite *old* scripts but use jquery when *similar problems come up again*. jquery is a best practice and a better approach. by your logic, there would *never be a good time* to take 2 minutes to try it. btw, a small design change like adding a field would break the getElementById script, but jquery is fine. why object to broadening someone's toolbox with a cheap and simple tool, especially since this is such a routine use-case for it.
jspcal
@jspcal In your answer to the OP, you didn't provide explanation, reasons why jQuery is good, or even why you were including another dependancy. Perhaps he is writing code for a company like one of my clients that allows *no* open source code to be used. I do not object to broadening someone's toolbox but it should be done with explanation and with caveats, if any, listed. Remember, I didn't downvote your answer, I promise!
Doug Neiner
@Doug Neiner: you're assuming a pathological case. best practices like jquery *should be the default.* more likely is the client prefers more efficient development and a more powerful system. jquery is *not* something that requires caveats - it's portable, widely-adopted, and the preferred solution. rather than pre-emptively eschewing frameworks (in anticipation of the unlikely requirement that the client *hates industry standards*, or 56k cached would become a burden), it should be recommended as a good approach for this class of problems.
jspcal
@jspcal jQuery is not a "best practice" any more than MooTools or Dojo or any other framework for that reason. It currently has the greatest adoption, but that of course could always change. *Now,* regardless of what is and is not the best, you have been around here long enough to know that giving jQuery in answer to a question not tagged "jquery" will get you a downvote unless you provide your reasoning *in* your answer. Not saying it is right, just saying that is how it is.
Doug Neiner
@Doug Neiner: jquery and using a framework *are* best practices (as an example, SO uses the same googleapis script i referenced). if the OP doesn't know about jquery, how do you expect him to *tag* it? note, jquery *is* javascript, so even based on the tags it's highly relevant, and ultimately the solution is apropos. by your argument, you couldn't mention any commonly-used library (say zend framework in php or boost in c++) without a lengthy disclaimer in a question tagged by language, which wouldn't be a very good policy.
jspcal
@jspcal You *cannot* call jQuery a "best practice" without discounting every existing JS framework. I agree that using *a* framework (for most normal websites) is a best practice, no argument there.
Doug Neiner
@Doug Neiner: so you agree. and don't misrepresent what I said. jquery is *one way to achieve best practices* (it happens to be the dominant method). the key is that using *a framework* is obviously better than using none at all. under no circumstances should a correct answer that recommends best practices be downvoted. that said, i'm fine with your answer, i just think that in general, jquery (or insert framework of your choice) is a better option. (note if A is a best practice, and B is an instance of A, B is also a best practice. that should be obvious.)
jspcal
+1  A: 

First, you need your id values to be different. You should never have the same ID twice on the same page. So lets use this as the example HTML:

<input type="text" id="name_0"  name="name" />
<input type="text" id="phone_0" name="phone" />
<input type="text" id="email_0" name="email" />

You could use this JavaScript function:

<script type='text/javascript'>
  function clearRow(id){
     var name  = document.getElementById('name_' + id),
         phone = document.getElementById('phone_' + id),
         email = document.getElementById('email_' + id);

     // Clear values
     name.value = phone.value = email.value = "";
  }
</script>

And your img tag would remain unchanged:

<img src="images/delete_row.png" width="25" onClick="clearRow(0);" />
Doug Neiner
works perfect, thank you!
lewisqic
+1  A: 

I have three input text fields that all have the id of "0".

This is entirely wrong. In a document you can't have element with the same id. Either use a name or a classname for these textfields and make their ids different.

<script type="text/javascript">
    function Change()
    {
        var elems = document.getElementsByName ( "myfields");
        for ( var i = 0;i < elems.length; i++)
        {
            elems[i].value = "";
        }
    }
</script>
<input name="myfields" type="text" id="txt1" />
<input name="myfields" type="text" id="txt2" />
<input name="myfields" type="text" id="txt3" />
<img onclick="Change();" alt="test" src="yourimagpath" />
rahul