Solved

Remove values from text box using onclick function

Posted on 2011-03-24
11
463 Views
Last Modified: 2013-11-19
Hi

I m having a issue, where i want to remove values from text box using onclick function...
i pasting the code here... in the code u can see other onclick function which is for other purpose...the deselect image is given onclick functions...
Another thing you can also assume that if same textbox values is required for onclick function on image... we can fetch it from database...

Actually this part comes in edit page... where user wants to reselect few things or user can remove the old selections by clicking the deselect image icon

Just go through the code and help me out..i m attaching the deselect image too... or u can use ur own...

<p>
  <input type="text" name="values" value=",2,3,4,6,7" id="textfield" />
</p>
<p> <div>
                            <ul>
                                                            <li title="Zimbabwe" id="right_country_63" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_63').hide();$('#left_country_63').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">Zimbabwe</p>
                                <br clear="all">
                              </li><li title="Zambia" id="right_country_47" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_47').hide();$('#left_country_47').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">Zambia</p>
                                <br clear="all">
                              </li><li title="Yemen" id="right_country_56" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_56').hide();$('#left_country_56').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">Yemen</p>
                                <br clear="all">
                              </li><li title="Vietnam" id="right_country_67" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_67').hide();$('#left_country_67').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">Vietnam</p>
                                <br clear="all">
                              </li><li title="Venezuela" id="right_country_42" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_42').hide();$('#left_country_42').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">Venezuela</p>
                                <br clear="all">
                              </li><li title="United States" id="right_country_19" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_19').hide();$('#left_country_19').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">United States</p>
                                <br clear="all">
                              </li><li title="United Kingdom" id="right_country_76" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="$('#right_country_76').hide();$('#left_country_76').show().find('input').attr('checked', false);check_null('country');"></p>
                                <p class="pp-rightpanel">United Kingdom</p>
                                <br clear="all">
                              </li>
</ul></div></p>

Open in new window


deselect image:  deselect icon deselect icon
0
Comment
Question by:global_expert_advice
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35211905
you mean I click on the first one and I remove 2 in the textbox?
0
 
LVL 9

Accepted Solution

by:
iGottZ earned 500 total points
ID: 35214040
this is my test code. i suggest you to try it exactly as i've posted it here in order to see how i made your wish work.
<html>
<head>
<script>
function remval (nr, that) {
  var values = document.getElementsByName("values")[0];
  var varr = values.value.split(",");
  for (var i = 0; i < varr.length; i++) {
    if (parseInt(varr[i], 10) == nr) {
      varr.splice(i, 1);
      break;
    }
  }
  values.value = varr.join(",");
  var nli = that.parentNode.parentNode;
  nli.parentNode.removeChild(nli);
}
</script>
</head>
<body>
<p>
  <input type="text" name="values" value="1,2,3,4,5,6,7" id="textfield" />
</p>
<p> <div>
<ul>
       <li title="Zimbabwe" id="right_country_63" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(1,this);"></p>
    <p class="pp-rightpanel">Zimbabwe</p>
    <br clear="all">
  </li><li title="Zambia" id="right_country_47" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(2,this);"></p>
    <p class="pp-rightpanel">Zambia</p>
    <br clear="all">
  </li><li title="Yemen" id="right_country_56" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(3,this);"></p>
    <p class="pp-rightpanel">Yemen</p>
    <br clear="all">
  </li><li title="Vietnam" id="right_country_67" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(4,this);"></p>
    <p class="pp-rightpanel">Vietnam</p>
    <br clear="all">
  </li><li title="Venezuela" id="right_country_42" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(5,this);"></p>
    <p class="pp-rightpanel">Venezuela</p>
    <br clear="all">
  </li><li title="United States" id="right_country_19" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(6,this);"></p>
    <p class="pp-rightpanel">United States</p>
    <br clear="all">
  </li><li title="United Kingdom" id="right_country_76" style="display:;">
    <p><img src="deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="remval(7,this);"></p>
    <p class="pp-rightpanel">United Kingdom</p>
    <br clear="all">
  </li>
</ul></div></p>
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:global_expert_advice
ID: 35214059
yes you are right...
actually the input box is having values of respective countries...
so suppose you click zambia it should remove the value of 47 from the input box...if its there in the input box...

my country table structure is

id   country
1     india
2     USA
3     YEMEN
47   ZAMBIA

hope you got it...

or else if you are confused of above code...
you can explain with ur code also
0
 
LVL 1

Author Comment

by:global_expert_advice
ID: 35214078
Hope you get idea from below screenshot...
 sample
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:global_expert_advice
ID: 35218205
hi
Thanks a lot... your rule worked perfectly.... but just got stuck at one time... as you see in the above image... when country goes from left to right... it adds its value... and when you deselect from right box... value gets removd from input box and again checkbox comes in left box...

however i said earlier that... i m also using other onclick functions to move countries between boxes...
now when i use your onclick function, it works and removes value... but again when you try to add same country from left to right... values gets added as per my function... but country don't show up in right box...

i m putting here the code for right and left box...with my javascript and ur... i know i m making somewhere mistake but not able to find..
<script>
function list4(c,n,z)
{
	s=document.InstantSMS1.religiondata.value;
	if(c.checked)
	{
		if(s.indexOf(n)<0) s+=','+n;
	}
	else
	{
		s=document.InstantSMS1.religiondata.value.replace(','+n,'');
	}
	z=",";

	if (s.substring(2) == z) s=s.substring(2);
		document.InstantSMS1.religiondata.value=s;
}

function clear_all(field, gender){
	$("li[id^='right_"+field+"_']").hide();
	$("li[id^='left_"+field+"_']").show().find('input').attr('checked', false);
	$("#right_"+field+"_dm").show();
	$("#data_"+field+"_part").val('');

	if(field == 'mothertongue' || field == 'community'){
		display_caste_option_new('', gender);
	}
}

function removal (nr, that) {
  var religiondata = document.getElementsByName("religiondata")[0];
  var varr = religiondata.value.split(",");
  for (var i = 0; i < varr.length; i++) {
    if (parseInt(varr[i], 10) == nr) {
      varr.splice(i, 1);
      break;
    }
  }
  religiondata.value = varr.join(",");
  var nli = that.parentNode.parentNode;
  nli.parentNode.removeChild(nli);
}
</script>

<table width="100%" border="0" cellspacing="0" cellpadding="4">
                  <tr>
                    <td width="150" valign="top">Religion:</td>
                    <td width="683" valign="top"><div style="float:left;width:480px">
                        <div class="available-options"> <span>Available options</span>
                          <div>
                            <ul>
                              <li style="background-color:#f2f2c3;white-space:nowrap;margin-bottom:2px">Frequently Used</li>
                                                            <li title="Hindu" id="left_religion_1"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_1"  value="1" 
 	       onclick="$('#left_religion_1').hide();$('#right_religion_1').show();$('#right_religion_dm').hide(); list4(this,1)" />
                                </p>
                                <p class="pp-rightpanel">Hindu</p>
                                <br clear="all">
                              </li>
                                                            <li title="Muslim" id="left_religion_2"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_2"  value="2" 
 	       onclick="$('#left_religion_2').hide();$('#right_religion_2').show();$('#right_religion_dm').hide(); list4(this,2)" />
                                </p>
                                <p class="pp-rightpanel">Muslim</p>
                                <br clear="all">
                              </li>
                                                            <li title="Christian" id="left_religion_3"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_3"  value="3" 
 	       onclick="$('#left_religion_3').hide();$('#right_religion_3').show();$('#right_religion_dm').hide(); list4(this,3)" />
                                </p>
                                <p class="pp-rightpanel">Christian</p>
                                <br clear="all">
                              </li>
                                                            <li title="Sikh" id="left_religion_4"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_4"  value="4" 
 	       onclick="$('#left_religion_4').hide();$('#right_religion_4').show();$('#right_religion_dm').hide(); list4(this,4)" />
                                </p>
                                <p class="pp-rightpanel">Sikh</p>
                                <br clear="all">
                              </li>
                                                            <li title="Parsi" id="left_religion_5"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_5"  value="5" 
 	       onclick="$('#left_religion_5').hide();$('#right_religion_5').show();$('#right_religion_dm').hide(); list4(this,5)" />
                                </p>
                                <p class="pp-rightpanel">Parsi</p>
                                <br clear="all">
                              </li>
                                                            <li title="Jain" id="left_religion_6"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_6"  value="6" 
 	       onclick="$('#left_religion_6').hide();$('#right_religion_6').show();$('#right_religion_dm').hide(); list4(this,6)" />
                                </p>
                                <p class="pp-rightpanel">Jain</p>
                                <br clear="all">
                              </li>
                                                            <li title="Buddhist" id="left_religion_7"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_7"  value="7" 
 	       onclick="$('#left_religion_7').hide();$('#right_religion_7').show();$('#right_religion_dm').hide(); list4(this,7)" />
                                </p>
                                <p class="pp-rightpanel">Buddhist</p>
                                <br clear="all">
                              </li>
                                                            <li title="Jewish" id="left_religion_8"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_8"  value="8" 
 	       onclick="$('#left_religion_8').hide();$('#right_religion_8').show();$('#right_religion_dm').hide(); list4(this,8)" />
                                </p>
                                <p class="pp-rightpanel">Jewish</p>
                                <br clear="all">
                              </li>
                                                            <li title="Spiritual" id="left_religion_9"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_9"  value="9" 
 	       onclick="$('#left_religion_9').hide();$('#right_religion_9').show();$('#right_religion_dm').hide(); list4(this,9)" />
                                </p>
                                <p class="pp-rightpanel">Spiritual</p>
                                <br clear="all">
                              </li>
                                                            <li title="Other" id="left_religion_10"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_10"  value="10" 
 	       onclick="$('#left_religion_10').hide();$('#right_religion_10').show();$('#right_religion_dm').hide(); list4(this,10)" />
                                </p>
                                <p class="pp-rightpanel">Other</p>
                                <br clear="all">
                              </li>
                                                            <li title="No Religion" id="left_religion_11"  >
                                <p>
                                  <input name="religionarray[]" type="checkbox" id="check_11"  value="11" 
 	       onclick="$('#left_religion_11').hide();$('#right_religion_11').show();$('#right_religion_dm').hide(); list4(this,11)" />
                                </p>
                                <p class="pp-rightpanel">No Religion</p>
                                <br clear="all">
                              </li>
                                                          </ul>
                          </div>
                        </div>
                        <input type="text" name="religiondata" id="data_religion_part" value=",2" />
                        <div class="my-selection"> <span><a class="link" style="float:right;" href="#" onclick="clear_all('religion', '');return false;">Clear</a>My selection(s)</span>
                          <div>
                            <ul>
                              <li title="Muslim" id="right_religion_2" style="display:;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px;" onclick="removal(2,this);$('#right_religion_2').hide();$('#left_religion_2').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Muslim</p>
                                <br clear="all">
                              </li>
                                                            <li title="Hindu" id="right_religion_1" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_1').hide();$('#left_religion_1').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Hindu</p>
                                <br clear="all">
                              </li>
                                                            <li title="Muslim" id="right_religion_2" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_2').hide();$('#left_religion_2').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Muslim</p>
                                <br clear="all">
                              </li>
                                                            <li title="Christian" id="right_religion_3" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_3').hide();$('#left_religion_3').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Christian</p>
                                <br clear="all">
                              </li>
                                                            <li title="Sikh" id="right_religion_4" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_4').hide();$('#left_religion_4').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Sikh</p>
                                <br clear="all">
                              </li>
                                                            <li title="Parsi" id="right_religion_5" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_5').hide();$('#left_religion_5').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Parsi</p>
                                <br clear="all">
                              </li>
                                                            <li title="Jain" id="right_religion_6" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_6').hide();$('#left_religion_6').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Jain</p>
                                <br clear="all">
                              </li>
                                                            <li title="Buddhist" id="right_religion_7" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_7').hide();$('#left_religion_7').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Buddhist</p>
                                <br clear="all">
                              </li>
                                                            <li title="Jewish" id="right_religion_8" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_8').hide();$('#left_religion_8').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Jewish</p>
                                <br clear="all">
                              </li>
                                                            <li title="Spiritual" id="right_religion_9" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_9').hide();$('#left_religion_9').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Spiritual</p>
                                <br clear="all">
                              </li>
                                                            <li title="Other" id="right_religion_10" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_10').hide();$('#left_religion_10').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">Other</p>
                                <br clear="all">
                              </li>
                                                            <li title="No Religion" id="right_religion_11" style="display:none;">
                                <p><img src="images/deselect-icon.gif" width="10" border="0" style="margin-right:2px" onclick="$('#right_religion_11').hide();$('#left_religion_11').show().find('input').attr('checked', false);check_null('religion');"></p>
                                <p class="pp-rightpanel">No Religion</p>
                                <br clear="all">
                              </li>
                                                          </ul>
                          </div>
                        </div>
                      </div></td>
                  </tr>

Open in new window

0
 
LVL 1

Author Comment

by:global_expert_advice
ID: 35218310
for first time name and value goes from left to right..
but second when you try are deselecting from right box..
only value goes... but not name....
0
 
LVL 9

Assisted Solution

by:iGottZ
iGottZ earned 500 total points
ID: 35312169
well it disappears completely because

  var nli = that.parentNode.parentNode;
  nli.parentNode.removeChild(nli);

is removing the whole node.

if you remove that you can also rewrite function remval (nr, that) { to function remval (nr) {
and remval(1,this); to remval(1);

thought you understood what the code did.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36128444
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split in Javascript 5 30
Problem with a CSS in a page 2 26
How can i create a customized Login page based on Wordpress ? 8 27
jquery conditionally populate list boxes 2 21
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now