get the current element in (inline function in JavaScript)? [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 1 year ago.
Improve this question
I am trying to get the element itself through making a function in the div itself I want when someone clicks on the current Element the Element should be deleted by fading in pure JavaScript I have searched on the internet but not getting the answer there are many similar questions on Stack Overflow but none of them giving me the combined answer(Like getting current element through Js in (inline function)).
So anyone can help me
<div style="width: 100px;height: 100px;background:red;" onclick="(function(ele){
console.log('helo')
console.log(ele.currentTarget)})();">
Thanks in advance

This should do it:
I added an id to your div which can be accessed through the getElementById.
<div id='hello' style="width: 100px;height: 100px;background:red;" onclick="
(
function() {
document.getElementById('hello').style.opacity = 0;
document.getElementById('hello').style.transition = '0.5s';
}
)();">

Related

Get the ID of element using the starting word of string present in it using 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 last year.
Improve this question
I know the starting word of the string present in the h2 element. How to find the ID of that element using contains?
The string present inside h2 is 'What Repairs Can Fix A P0500 Trouble Code?'. The sub string 'P0500' varies so it is not known. Is there a way to find the id using only the known words of the string.
const word = 'P0500';
let id = $(`h2:contains(${word})`).attr('id');
console.log(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<h2 id="heading2">What Repairs Can Fix A P0500 Trouble Code?</h2>
It's working fine for me. I added a working snippet for the reference.
let id = $('h2:contains("test")').attr('id');
console.log(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2 id="1">div1</h2>
<h2 id="2">This is a test, nested in div1</h2>
<h2 id="3">Nested in div1<h2>

Get id from class selector without click [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to get the id from a class selector without clicking.
var type_selected = '' ;
$('.type_selected').attr();
type_selected = (this.id);
Use this. This will return an array of all elements with that class;
var type_selected = [];
$('.type_selected').each(function(){
type_selected.push($(this).attr("id"));
});
console.log(type_selected);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="type_selected" id="id1">This is a test<p>
<p class="type_selected" id="id2">This is a test 2<p>
<p class="type_selected" id="id3">This is a test<p>

How to get <option> from <select> by its index? [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 7 years ago.
Improve this question
I'm having here a <select> that looks like this:
<select id="stdin-select" class="io-byte-box" size="10">
<option>0x5232</option>
<option>0xFD13</option>
<option>0x13E7</option>
<option>0x1234</option>
<option>0xFFFF</option>
</select>
and I am trying to access the values by its index like this:
var stdinValue = $('.stdin-select.dropdown option').eq(0).text();
and this
var stdinValue = $('.stdin-select option').eq(0).text();
like described here but it's not working.
What am I doing wrong here?
Change class (.) to id ('#')
var stdinValue = $('#stdin-select option').eq(0).text();
In your example there is not element with class stdin-select, instead there is element with if stdin-select

CSS overriding JavaScript [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 am trying to make the contactform originally hidden when the page is loaded, so contactform is to be hidden originally to do this in CSS i did:
#contactform{
display: none;
}
now in the HTML I have:
<div class="contactme" >Message me!</div>
and onclicking the message me it calls the function 'someFunc' the function hides the contactinfo but displays the contactform, the function is:
function someFunc() {
document.getElementById('contactinfo').style.display='none';
document.getElementsByID('contactform').style.display='block';
}
It hides the contactinfo perfectly as expected, however it doesn't show the contactform. I believe this could be because the CSS is overriding the function, is there any way to stop it from doing so and get it working as expected?
There is bad function name, correctly is small "d" and "Element" instead of "Elements"
document.getElementById('contactform').style.display='block';
^ ^
in your code document.getElementsByID is mention not document.getElementById USE THIS code:
function someFunc() {
document.getElementById('contactinfo').style.display='none';
document.getElementById('contactform').style.display='block';
}

document.execCommand('bold',false,true) does not work with jquery on("click", function(){}) [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 6 years ago.
Improve this question
I am trying to do rich text editing with javascript and jquery.
I want to create a button to embolden selected text in a content editable div. When I tried with the following code, I get the expected result.
<div contenteditable="true" style="height:100px;width:100px;border:1px solid; " class="editor"></div>
<button class='bold' onclick="document.execCommand('bold',false,true);">B</button>
However, when I used jquery to do the same thing, it was not working.
<div contenteditable="true" style="height:100px;width:100px;border:1px solid; " class="editor"></div>
<button class='bold'>B</button>
$(function(){
$("button.class").on("click",function(){
document.execCommand('bold',false,true);
})
})
Why isn't jquery working for my problem ?
$("button.class").on("click",function(){
Change this to
$("button.bold").on("click",function(){
This is you can do and I hope this will definitely work.
$('.bold').click(function(){});

Categories