JavaScript innerHTML change onclick not working [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to change a piece of text with the click of a button, this doesn't work. Any ideas?
<div>
This text should stay, <span id="x">This text should change.</span>
</div>
<button type="button" onclick='change()'>Change</button>
<script type="javascript/text">
function change() {
document.getElementById('x').innerHTML='The text has changed.';
}
</script>
What did I do wrong?

Your type attribute is wrong. You don't really need it at all. If you do want to include it, change it to text/javascript.

Related

Why attr id not assigned to form? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Please advise why this code doesn't work. The ID of the form should appear in input field as value.
$("button").click(function() {
$("form").attr(id, "message");
$("input").val($("form").attr('id'));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="text">
</form>
<button>Check</button>
You need to use quotes for the id
$("form").attr("id", "message");

How do i add attribute to html tag with jQuery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a html page with <html> tag where I want to add dir="rtl" and lang="ar" attributes using jQuery when user click on arabic language in drop-down.
I tried using $(html).attr("dir","rtl")
But it returns undefined. Can somebody help as I'm new to jQuery
There is an error on your code, the "html" tag should be written with quotes as folows:
$('html').attr("dir","rtl")
Ok, the question is closed, answer given, so no use for this answer anymore.

Can't write html dynamically in Jquery innerHtml [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I use jquery to put html dynamically but it is not working...
Here is what I have tried
$("#root").innerHtml("<h1>Hello</h1>")
You should not use innerHtml instead use html()
You can do as below
$("#root").html("<div>Hello</div>")

Why is my custom data attribute not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Using jQuery
console.log($(this).html());
console.log($(this).data('section'));
yields
Include Children?
and
undefined
and I'm not sure what I'm doing incorrectly. The <a> here is inside an un-ordered list <li> tag, but I'm not sure why that should matter. Thanks in advance!
As you said it is <li> enclosing the <a>
then it should be:-
console.log($('li a').html());
console.log($('li a').data('section'));
Example:-
console.log($('li a').html());
console.log($('li a').data('section'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>Include Children?</li>
Note:- you can do $(this).find('a').data('section'); also based on your HTML structure.

Selecting class inside div in jquery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to select a class inside div.
my code:
<div id="cool" style="width: 80%;height: 80%;margin: auto;">
cool
</div>
Selector:
$('#cool .redirect').attr('href', google.com);
it does not work. Please suggest.
I'm a beginner and just started to learn jquery!
Your code works for me once I correct your attempt to use google.com (that is, the value of the field named com in the nonexistent variable google). Put quotation marks around the string and it works fine.

Categories