Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two controls whos code are run time rendered as below:
ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl03_txtTravelDate
ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl04_txtTravelDate
How do I fetch the values of above two controls using jQuery?
In asp.net , your control's id is changed at runtime.
If you id was mycontrol, it would be changed to blahblahblahmycontrol (id will be there at the end) at client side.
So,
if you have an asp.net control (textbox), like below,
<asp:TextBox runat='server' id='mytxtbox'></asp:TextBox>
you can do,
$("input[id$='mytxtbox']").val();
It selects all input tags ending with _mytxtbox
or you can do something like this if the javascript is on the same .aspx file
$('#<%=mytxtbox.ClientID %>').val()
If you are using asp.net 4.0,
You can add ClientMode property to Static
<asp:TextBox ID="mytxtbox" runat="server" ClientIDMode="Static"></asp:TextBox>
This will retain the id of TextBox to mytxtbox
So, you can do,
$('#mytxtbox').val();
Yes, you can also, see the source code and use the converted id like this,
var txtboxValue = $('#ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl03_txtTravelDate').val();
But its not recommended.
Simple - use .val():
var firstValue = $('#ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl03_txtTravelDate').val();
var secondValue = $('#ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl04_txtTravelDate').val();
You should seriously consider using shorted ids. From these ids alone, I have absolutely no idea what the value represents.
You can make it more readable with:
var baseID = "#ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_";
var firstValue = $(baseID + 'ctl03_txtTravelDate').val();
var secondValue = $(baseID + 'ctl04_txtTravelDate').val();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have very annoying problem, I need to display MySQL queries by using a PHP foreach loop but because of this when i try to set a javascript value for each of the items that are looped through it only selects either the first or last value it ignores the rest.
Here's what i've tried so far:
I have given an input a class and given it a value as well, this is within the for-each loop.
<input class="classs" value="{{ $item->id }}"/>
Next i have tried to access this from a js function, which is outside of the for-each loop.
function addSubTaskToDatabase(){
var itemId = $('.class').val();
alert(itemId);
}
Problem is all this does is alert the last value in the for-each loop with that class, its the same for name and for id also.
Anyone know the fix..? all help is appreciated.
var itemId = $('.class').val();
is going to select all elements on the page with that class.
You probably want a click function on the elements, and use this to access the currently-clicked item:
$('.class').click(function() {
var itemId = $(this).val();
alert(itemId);
});
To get all values you need an $().each loop
$('.class').each(function() {
var itemId = $(this).val();
alert(itemId);
}
see https://api.jquery.com/each/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am curious to know, how can i assign multiple html elements to a single variable in jquery...
for an example if i am having...
$('.some').drags();
$('.some1').drags();
at one instance i want variable drgoff
var drgoff = $('.some').offset().top;
at other instance i want variable drgoff
var drgoff = $('.some1').offset().top;
i am using this drgoff in the function, so now my question is how can i get all that html elements in place of .some when that particular html element is called...
var drgoff is not inside function it is a global variable..
thanx for any help...
it can be done using if else also but that will be too lengthy..
Use jQuery's .add(). var drgoff = $('.some').add('.some1');
Live demo here (click).
Well don't know when you are setting the value for drgoff. If you have a function (as indicated in question), then you can pass it the class name for which you want to get the value like this:
function getDragOffValue(cname){
dragoff = $(cname).offset().top;
}
and use it like:
getDragOffValue(".name");
alert(dragoff);//gets value for elements with class .name
getDragOffValue(".name1");
alert(dragoff);//gets value for elements with class .name1
But I don't understand how your code will behave if you have multiple elements with same class name. It will return the offset().top value for first element in collection of elements with given class. In short $(".name").offset().top is as good as $(".name:first").offset().top.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an html page with a lot of javascript code, for example the content of a div depends on length of an array :
for (var i = 0; i < movieList.length; i++) {
document.body.appendChild(document.createElement('h2')).appendChild(document.createTextNode('title: ' + movieList[i].title));
var cUL = document.body.appendChild(document.createElement('ul'));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].rating));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].year));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].length));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].isComedy));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode('main characters: ' + movieList[i].mainCharacters.join(", ")));
}
I am using these perl LWPx::ParanoidAgent and HTML::TokeParser modules to handle the HTML code but the i want the result of the javascript script
You either need to:
Reverse engineer the JS and apply the changes it would make manually or
Run the HTML and JS through a browser or browser-like tool and read the data from its DOM
There are a number of options for the latter, including WWW::Mechanize::Firefox, WWW::Selenium and Wight.
perhaps https://getfirebug.com/ is what you're looking for. Amongst loads of other things you can view your HTML after JavaScript has altered it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");