simple javascript question - javascript

document.getElementById("myFrame").setAttribute("src") = "http://www.yahoo.com/";
myFrame is an iframe element... when entering this into the chrome developer console, it gives me the error "Invalid left-hand side in assignment" I am trying to update the iframe. Is there a method I am forgetting?

setAttribute takes two arguments:
document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/");
You are trying to set the DOM object to the string "http://www.yahoo.com/" ... which is invalid.

You don't really need setAttribute when setting the src property:
document.getElementById('myFrame').src = 'http://www.yahoo.com/';

You can't assign an function call to something, try this instead:
document.getElementById("myFrame").setAttribute("src", http://www.yahoo.com/");

Here, I fixed it
document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/");

Try this...
document.getElementById("myFrame").setAttribute("src","http://www.yahoo.com/");

Related

Changing style of getElementsByClassName gives compile error

I am trying to change the style of all of the elements which I received by calling the method getElementsByClassName. The thing is that it does work when it has compiled before (I commented these rows out to let it compile), it just says the error in the cmd. After it compiled I just set the lines back to normal, they keep giving errors but are working in the browser. Any thoughts on this weird behavior?
When commented out:
When not commented and giving errors:
I think the problem is with TypeScript. You should try this workaround
var texts = document.getElementsByClassName("section_text") as HTMLCollectionOf<HTMLElement>;
I had a similar issue when trying to pull off something with angular. The problem is that the getElementsByClassName returns a nodelist, not a normal array, so you can't access properties like you're trying to. You can read more in about it in this answer.
you can simply addClass():
CSS
.newStyle{
width:100%;
float:left;
}
Use Jquery
$('.section_text input').addClass('newStyle');
Use Javascript
document.querySelector('.section_text input').classList.add('newStyle';
getElementsByClassName returns a nodelist. Access properties directly.
let hiddenLocales = document.getElementsByClassName('localeMatch') as HTMLCollectionOf<HTMLElement>;
let hideParentNode = hiddenLocales[0].parentElement;
hideParentNode?.classList.add('newStyle');

How to set attributeName to "value"

I'm trying to assign "value" as attributeName to a jQuery object. However, I am getting the "Object doesn't support this property or method" error. Here is the line:
$(rowElem).attr("value","");
I think I am getting this error because jQuery treats the word "value" as another method and not as an attribute. I will gladly appreciate help from you guys. Thanks in advance! :)
Hi you are doing it wrong , Jquery need a selector whether its id , class or tagname
for Id you need to add prefix "#" For Class you need to add prefix "."
and tag name as it is....
So your fixed code is something like that
$("#rowElem").attr("value","My New VAlue");
When ever you use value I think the only jquery you will use is
$(rowElem).val("");
It is however, not a good practice to use 'value' as any other attribute, what you can do is with HTML5, make your own attribute as
<tr data-value="somevalue" />
and use the attribute like
$(rowElem).attr("data-value", "");
and you can use that attribute's value like
var val = $(rowElem).attr("data-value");
There is nothing wrong with the code that we can see. It looks like you don't have jQuery loaded in the DOM. See my tests below.
i.e. If you go to backbonejs.org and type in the following in the console:
if(jQuery)
console.log('jQuery has loaded');
// logs 'jQuery has loaded'
var rowElem = 'div';
$(rowElem).attr('value', '');
It successfully adds 'value' to the divs.
However, if you use a site without jQuery like underscorejs.org, it won't work:
if(jQuery)
console.log('jQuery has loaded');
// ReferenceError: jQuery is not defined
var rowElem = 'div';
$(rowElem).attr('value', '');
// TypeError: Object #<HTMLDivElement> has no method 'attr'
You need to get it access to jQuery and your problem should be solved.
There is no such attribute in jquery or javascript. Both treats value as a separate function.
You can try using:
jquery: $(rowElem).val("urVal");
javascript: rowElement.value = "urVal";
use this.. provide value in val
$(rowElem).val("value");
hope it works...

Add non-standard attribute to a select tag with Javascript

I want to create a select element through JavaScript and I want to set an attribute to it called data-placeholder.
How can we assign non-standard attributes with JavaScript without the browser complaining with:
Uncaught referenceError: Invalid left-hand side in assignment
Code that cause the error:
select1.data-placeholder="Choose ...";
Since it says with javascript not jquery...
yourElement.setAttribute('data-placeholder','value');
Here's a pretty simple non-jQuery way to achieve this;
var el = document.createElement('select');
el.setAttribute('data-placeholder', 'placeholder value');
document.body.appendChild(el);​
I've created a very simple JSFiddle to demonstrate it.
Hope that helps!
JQuery makes this easy:
$("<select></select>").attr("data-placeholder", "value").appendTo($element);

Disabled is undefined javascript error

When I try to do this:
document.getElementById('idOfCheckBox').disabled = disabled; I get error : disabled is undefined.
What can be the reason?
Have you tried to see what document.getElementById('idOfCheckBox') gives you? Is it a HTMLDOMElement with an attribute disabled? If not, maybe you got the wrong element?
Try console.log(document.getElementById('idOfCheckBox')) if you have access to a fairly modern browser.
No such element exists, or
No such variable disabled exists in the current scope.
Pretty self-explanatory.

location.href in javascript?

javascript:
location.href("somefile.php"); // successfully working with IE
location.href = "somefile.php";
Ques 1. first code is not work with Safari. Why?
Ques 2. What is the difference b/w these codes.
Thanks,
href is not a method, it is a property whose value is a string.
The first is a call on a method with a url as the argument (incorrect), the second is assigning the url as the value of the property (correct).
See also: http://www.w3.org/TR/Window/#location
i never heard of location.href("somefile.php");... location.href = "somefile.php"; is the "normal" way that you should use.
Also it's more efficient using window.location than location. So try to use :
window.location.href = "somefile.php";
(as Andy said, href is a property and in JS you specify a property value in this way: object.property = "value")
Answer 1 :- It wont work because href is a property of location object, not a method.
Answer 2 :- location.href("...") denotes a method(that is invalid) while location.href is a property.

Categories

Resources