Identical ID's in the same HTML page - javascript

I have a page that has two different inputs that contain same ID but each one in different form. and I'm actually setting values to inputs from javascript using get element by ID. I know this is not valid. but the thing is if i change one of the input id's I'm gonna need to re write a bunch of code in 'shopping cart ' cuz these input's pass value to cart. I'm actually not planning to touch that for now. So, is there any trick that can target one input instead of the other even if they have the same id's??
ex:
<input type="hidden" name="cart_1_ID_Add2" id="cart_1_ID_Add2" value=""/>
<input type="hidden" name=cart_1_ID_Add2" id="cart_1_ID_Add2" value=""/>
thanks in advance!!

Although it is a wrong practice, and you should use different id's, you could add a different class attribute to each one.
<input class="input1" type="hidden" name="cart_1_ID_Add2" id="cart_1_ID_Add2" value=""/>
<input class="input2" type="hidden" name=cart_1_ID_Add2" id="cart_1_ID_Add2" value=""/>
var x = document.getElementsByClassName("input1")[0];
Again: I strongly recommend you to find time to change the logic of your program to use unique id's.

If they are in different forms you can target them by selecting IDs where they are inside a certain class. You can also change the name attribute and select that instead.
HTML
<div class="form1">
<input type="hidden" name="rename1" id="cart_1_ID_Add2" value="">
</div>
<div class="form2">
<input type="hidden" name="rename2" id="cart_1_ID_Add2" value="">
</div>
JS
$(".form1 #cart_1_id_add2")
//Or
$("input[name*='rename1']")
However you shouldn't really have the same id twice on one page otherwise it makes it hard to maintain and debug. If it's not a huge job to change your approach I'd recommend you do that.

Add another attribute to one or both tags.
For example, you can make them
<input type="hidden"name="cart_1_ID_Add2" id="cart_1_ID_Add2" data-id="add100" class="myInput" value=""/>
<input type="hidden" name=cart_1_ID_Add2" id="cart_1_ID_Add2" data-id="add101" class="myInput" value=""/>
Then get their values with jQuery
var myInput = $('[data-id=add100]').val();
console.log(myInput);
OR use plain javascript by adding a class and getting the value
var myVal = document.getElementsByClassName("myInput")[0];
console.log(myVal.value);
Hope this helps

Yes.
That code shouldn't have duplicate id properties in Dom btw.
You can use a custom HTML element property:
<input type="hidden" name="cart_1_ID_Add2" id="cart_1_ID_Add2" value="" my-property="some_identifier"/>
<input type="hidden" name=cart_1_ID_Add2" id="cart_1_ID_Add2" value="" my-property="some_identifier2"/>

Related

How to create auto-input script based on agularJS?

I´m trying to implement a greaskemonkey script to make an auto-input, but I cannot find a way to do it.
What I have:
HTML form:
<form ng-submit="buy(quantity2)">
<input name="quantity" type="text" ng-model="my.quantity" style="width:30px" maxlength="2">
</form>
I simply don´t know how to input a value for the box, usually I would do
$("input[name='quantity']:first").val("1");
Unfortunately val doesn´t exists here. Need a help, thanks!
For your better understand i just give you a example how you can take your value.
HTML form:
<form ng-submit="buy(youravlue)">
<input name="quantity" id="quantity" type="text" ng-model="youravlue" style="width:30px" maxlength="2">
</form>
using ng-submit you can take your value this way.
$scope.buy=function(data){
console.log(data);
}
using ID you can take your value this way.
angular.element("#quantity").val();
In angularjs we have to find the element either by id or querySelector or querySelectorAll and wrap it over angular.element which will provide jqlite(lighter version of jquery)
Refer this https://docs.angularjs.org/api/ng/function/angular.element
angular.element(document.querySelector("input[name='quantity']")).val("1");

How to get value of input field which is an array, by it's name using javascript

Below is my code,I want to get values of p_id by using it's name. As i am new to javascript,please help me.
<form>
<input name="p_id[]" value="0"/>
<input name="p_id[]" value="1"/>
<input name="p_id[]" value="2"/>
</form>
You can use pure javascript method:
document.getElementsByName('p_id[]');
To get values of a particular one :
document.getElementsByName('p_id[]')[0].value;
You may want to try like this:
document.getElementsByName("p_id[]")[0].value;
Try this fiddle:
http://jsfiddle.net/qhmohco5/
<form>
<input name="p_id_1[]" value="0"/>
<input name="p_id_2[]" value="1"/>
<input name="p_id_3[]" value="2"/>
</form>
Since you tagged JQuery:
$('input[name*="p_id"]').each(function(){
console.log($(this).attr('value'))
})
Fiddle
This function finds all the input tags with the name containing the phrase p_id and logs their values.
The *= operator performs substring matching, so you can specify a pattern that you want to match.

use .search() or regex to find elements that contain X in the ID

I have a group of input boxes that are dynamically built and added to the page. I can control the ID that is placed on the elements but it is wrapped in with a bunch of garbage. For example, I give it an ID of clientTest it will render an id of j_id0:j_id2:theForm:clientTest_mod. There is a total of 7 input boxes that contain this Id but contain different endings. The first part of the ID is also dynamic so I can not hard code anything in.
An example,
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_lkid" id="j_id0:j_id2:theForm:clientTest_lkid" value="000000000000000">
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_lkold" id="j_id0:j_id2:theForm:clientTest_lkold" value="null">
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_lktp" id="j_id0:j_id2:theForm:clientTest_lktp" value="001">
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_lspf" id="j_id0:j_id2:theForm:clientTest_lspf" value="0">
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_lspfsub" id="j_id0:j_id2:theForm:clientTest_lspfsub" value="0">
<input type="hidden" name="j_id0:j_id2:theForm:clientTest_mod" id="j_id0:j_id2:theForm:clientTest_mod" value="0">
How can I search through and find the input boxes that I want to target?
extending #Michael Chaney
// select the collection
var inputs = $("input[id*='theForm']");
// loop through each element.
$("input[id*='theForm']").each(function(ind, ele){
$("#textPlace").append("<li>"+ $(ele).attr("id")+"</li>");
});
http://jsfiddle.net/1zsj0zcc/1/

Passing between two hidden fields

I seem to have an issue with passing a value between two hidden fields.
I'm using a jquery plugin for for geolocation from google maps. The plugin uses a fieldset which includes two hidden fields, one for lat and one for lon. Each time then pin is moved the two values change, which is definitely happening and can be viewed in the developer tools.
I have a form that I'm bringing all of my values into to be sent to a php file all in one go. I've managed to get it working for all the other inputs I need but this one just wont work.
Heres the map with the two hidden fields. The hidden classes come with a value of 20 preset which changes.
<div class="gllpMap">Google Maps</div>
<input type="hidden" class="gllpLatitude" value="20"/>
<input type="hidden" class="gllpLongitude" value="20" />
<input type="hidden" class="gllpZoom" value="3"/>
</div>
And here is my search that is going to be sent to the php.
<form method="post" action="getData.php">
<input type="text" name="username" class="mySearch" value="" />
<input type="hidden" name="fromTest" id="fromTest"/>
<input type="hidden" name="untilTest" id="untilTest"/>
<input type="hidden" name="lat" id="lat"/>
<input type="hidden" name="long" id="long"/>
<input type="submit" class="myButton" value="">
</form>
For some reason I just cant get this to work. What I'm looking for is the simplest way to get this lat and long to equal the values set by the two hidden fields. The simpler the better, I'm pretty new to coding and this is just a first mock-up which I hope to revisit properly when I have more time available.
You can take and set the value using JQuery .val()
$("#lat").val($(".gllpLatitude").val());
$("#long").val($(".gllpLongitude").val());
DEMO

Multiple Classes jquery

I have multiple classes with different values. The values are generated from MySql. How do I get the value of all the 'VALUEs' i.e 1,2,3,4 in order to post it to my query? Each input has its own button and form. If i click the first button i want it to post 1, then the next button and form will post 2.
<input type="hidden" class="hideID" name="id" value="1">
<input type="hidden" class="hideID" name="id" value="2">
<input type="hidden" class="hideID" name="id" value="3">
<input type="hidden" class="hideID" name="id" value="4">
My jquery code currently only gets the first value (1):
$('.addToCart').click(function(){
var hideID = $('.hideID').val();
alert(hideID);
});
you need a loop for this. $(".hideID") is returning an array with all your elements with the class "hideID". so you need something like this to read all values
$.each($(".hideID"),function(index,element){
console.log($(element).val());
});
This should return all the values of your hidden fields in alert popups but maybe you should give them all different names. name="id" might conflict somewhere down the line.
$('.addToCart').click(function(){
$.each($('.hideID'),function(index,element){
alert($(element).val());
});
});
You can play around with it on jsFiddle.

Categories