search throught the html code for an element by name - javascript

i tried to search throught the html code for an element named scope using javascript. After that it should show a alertbox with the value of this element.
if($('[name=scope]').length > 0){
var scope = $(this).value();
alert(scope);
}
But that doesn't work for me. What can i do to fix this?
Thanks!

You need val() instead of value(), there is not value function in jQuery
var scope = $(this).val();
Or simply use this.value, that would be simple and faster
var scope = this.value;
Edit based on comments
$('[name=scope]').each(function(){
alert(this.value);
});

Related

How can I use Javascript to get the value of an element that was populated by a POST request?

I'm building an HTML page that receives data from another page with the below code
$arrayPosition = $_POST['arrayPosition'];
echo '<span id = "arrayPosition">'.$arrayPosition.'</span>';
I'm then trying to use javascript to get the value of the element and pass it to a function with the below code
var initialPosition = document.getElementById('arrayPosition').value;
function displayWork(position){
$("#displayArtwork").detach()
.append(holdImages[position])
.hide()
.fadeIn("fast");
}
When I alert the value of initial position to the screen it informs me that null is its value, however, when I inspect the element it looks like this
<span id="arrayPosition">4</span>
Am I making some really stupid error, or misunderstanding the way to access this posted data?
Thanks for your help!
Since arrayPosition is a span, it has no value. You can get its innerHTML:
var initialPosition = document.getElementById('arrayPosition').innerHTML;
Or using jQuery:
var initialPosition = $('#arrayPosition').text();
A span-element has no value. Only form-elements can contain the value-attribute. To get the text inside your span you can use the innerHTML-porperty:
var initialPosition = document.getElementById('arrayPosition').innerHTML;
Demo
As you are already using jQuery you can also use it's text()-function:
var initialPosition = $('#arrayPosition').text();
here you can also use:
$(document.getElementById('arrayPosition')).text();
Harder to maintain and more difficult to read but faster than the jQuery-Selector. (see here)
Demo 2
Reference
.innerHTML
.text()

Trying to replace a html attribute using a click function

I'm trying to take an a element and take the information from the data-id attribute and store it into a variable. I want to then splice this variable so I get the part that I need and implement it into another variable with some text. From that I then want to replace the src attribute of an iframe with the variable. Unfortunately, this doesn't seem to be working at all and I can't find the issue.
Here is the code:
$('.watchvideo').click(function(){
var idu = $(this).attr('data-id');
var id = "//www.youtube.com/embed/"idu.substr(27,37);
$('.videofeatem').setAttribute("src", id);
});
You have 2 issues in code:
1) concatenating substring
2) setting attribute via jquery
var id = "//www.youtube.com/embed/"+idu.substr(27,37);
$('.videofeatem').attr("src", id);
Without seeing the HTML, it's tough to be sure, but a + fixes the obvious problem:
$('.watchvideo').click(function(){
var idu = $(this).data('id');
var id = "//www.youtube.com/embed/" + idu.substr(27,37);
$('.videofeatem').attr("src", id);
});
Also, note that data-xxx attributes can be read by jQuery as .data('xxx')
Simply.
var id = "//www.youtube.com/embed/" + idu.substr(27,37);
Since you're using jQuery, use the .attr() method instead of the .setAttribute().

Change textbox value in javascript

I want to update the value of text box from where I'm getting initial value to add product to cart.
Now I'm applying round function & I want to update the value of text box that is because I'm using ajax & if I'm applying round function, user must know how system calculated everything.
Here's my simple code:
<script>
$(document).ready(function(){
$(document).delegate('.purchasenow','click', function(e){
var buynow=this;
var productid = $(this).attr('id');
var quantity = $('.quo_'+productid).val();
var quantity = Math.round(quantity);
alert(quantity); //This gives the value
$('.quo_'+productid).value = quantity; //This is not working
});
});
Can anyone tell why it's not working? It's very simple but I'm not able to find out the cause.
Have you tried
$('.quo_'+productid).val(quantity);
The jQuery selector returns a wrapped object, not the actual DOM element. I don't think wrapped object has the .value property
you are almost correct the only thing why it is not working because your syntax is wrong
this is the correct syntax for jQuery
$('.quo_'+productid).val(quantity);
if you want it in javascript
var txt = document.getElementById("quo_"+productid);
txt.value = quantity;

How to get value of JavaScript into HTML span?

If I have a much simplified JavaScript method such as this:
function myfunction() {
var i = 9;
}
Is there any way I can get the value of i into HTML such that when the web page with this method is called, the value 9 is displayed in a div or span element?
You can write document.getElementById("some ID").innerHTML = i
Hi :D you can try the following using JQuery:
var i = 9;
$('#spanId').text(i);
or the classic old-fashion way:
var tmp= document.getElementById('spanId');
tmp.textContent = id;
I hope this helps.
You can use innerHTML to embed the i value in a div or span element.
myfunction() current does not return its value. However, if you want to get the value when the page is "called" (loaded) you can do this:
function myfunction() {
var i = 9;
return i;
}
And in the markup:
<body onload="document.getElementById('id_of_your_div').innerHTML = myfunction()">
Please note that innerHTML has cross-browser issues, so you may want to use a library function such as jQuery's html() for reliable results.
Yes, you can assign an id to your <div> or <span> and set its innerText/textContent property to your value.
window.onload = function() {
var content = myfunction();
var tag = document.getElementById('displayVar');
if(typeof tag.innerText == "undefined") {
tag.textContent = content;
} else {
tag.innerText = content;
}
}
Do not use innerHTML if you do not want the HTML code of your value to be parsed (or if you don't expect any HTML value).
And no, you do not need a 31kb library to do that kind of work (just in case there's a bunch of "jQuery can do that!" answers).
Note that you must also modify myfunction() so that it returns the current value. A simple return i; statement in the function will do the trick.
JavaScript:
function myFunction() {
var i = 9;
return i;
}
$('myElement').innerHTML = myFunction();
HTML:
<span id="myElement"></span>
you can also use jQuery to simplify the syntax following are the three ways you can do that using jQuery,
1) $('span').text(i)
2) $('#someid').text(i) //someid = value of id attribute of span tag.
3) $('.someclassname').text(i) //someclassname = class name for the span tag.
Also using jQuery means forcing the users have to download addition jQuery lib when the page loads. That might slow down the page depending on the connection speed of the users. You might want to consider that while selecting between jQuery and plain JavaScript

How to get selected value from dropdownlist in asp.net using Javascript?

I am populating country dropdownlist from a database. I need to select a value from dropdown list and assign it to textbox by using Javascript.
Code:
var textboxId = document.getElementById("txtCountry");
var dropdownListId =document.getElementById("ddlLocation");
var e = document.getElementById("ddlLocation");
var strUser = e.options[e.selectedIndex].value;
document.getElementById(textboxId).value = strUser;
document.getElementById(textboxId).focus();
by doing this I am getting error. Any solutions?
Your code is wrong, Look at where I've made the changes to the same code:
var textboxId = document.getElementById("txtCountry");
var e = document.getElementById("ddlLocation");
var strUser = e.options[e.selectedIndex].value;
textboxId.value = strUser;
textboxId.focus();
What you did, is you selected your textbox and JS returned you a DOM element of that text box and you wanted to populate it by passing the DOM of the textBox inside the getElementById() function.
Here is where it broke:
document.getElementById(textboxId).value = strUser;
To use getElementById() method, you pass a string value of the id of an element.
Hope this helps.
Try with:
document.getElementById('<%=txtCountry.ClientID%>').value
or
var textBox = document.getElementById('<%=txtCountry.ClientID%>');
textBox.value = strUser;
That's because the ids of the html elements in the generated documents doesn't match with the id that you have assigned in your code. To get the id assigned to your control in the html, you can use the ClientID property of your dropdown.
Another problem is that you assign yourhtml element to variable and then use getElementById function which is not valid call.
This is changed in ASP.NET 4, that is about to be released.
Hope that helps!
These two lines:
document.getElementById(textboxId).value = strUser;
document.getElementById(textboxId).focus();
are wrong too. If your previous line actually worked:
var textboxId = document.getElementById("txtCountry");
then what you have called textboxId will actually be the textbox control, so you will be doing a getElementById using the control instead of a string identifier.
To follow upon what #anthares said; try this:
var textboxId = '<%=txtCountry.ClientID%>';
alert('My textbox id is: ' + textboxId);
and make sure that you are getting the correct ID for the textbox (remember that it will be munged by ASP.Net, at least make sure you are not getting nothing). Then when you do a document.getElementById you need to check the result before using:
var myTextBox = document.getElementById(textboxId);
if (myTextBox !== null) {
...now i can access the properties...
}
If you don't want to use
document.getElementById()
try it:
var VarName = $('#<%=YouDropDownId.ClientId %>').val();

Categories

Resources