Getting the id value - javascript

I'm working with JavaScript & FrameWork7.
I am using the accordion element, and when I open the accordion I have to "identify" which item was opened, so I have on a variable the html element but I don't know how cand I get the "id" value
I'm using this to get the "element" open
app7.on('accordionOpen',function (el) {
console.log(el);
};
So, the console print this..
<li class="accordion-item">
<a class="item-content">
<div class="item-inner">
<div class="item-title" id="numero_jornada">Jornada 1</div>
</div>
</a>
<div class="accordion-item-content">
<div class="block" id="show-resul-jor-1">
</div>
</div>
</li>
How can I get the value for the Id="numero_jornada" ?
Could you help me, please?

try to use document.getElementByID("numero_jornada").value;

If your el is a normal HTMLElement, you can simply use el.id as its is in the HtmlElementNode Interface

works, thanks
let titulo = el.querySelector('.item-title');
// Mostrar ID del título
console.log(titulo.id); // numero_jornada
// Mostrar contenido del título
console.log(titulo.textContent); // Jornada #

Related

Copy HTML from element, replace text with jQuery, then append to element

I am trying to create portlets on my website which are generated when a user inputs a number and clicks a button.
I have the HTML in a script tag (that way it's invisible). I am able to clone the HTML contents of the script tag and append it to the necessary element without issue. My problem is, I cannot seem to modify the text inside the template before appending it.
This is a super simplified version of what I'd like to do. I'm just trying to get parts of it working properly before building it up more.
Here is the script tag with the template:
var p = $("#tpl_dashboard_portlet").html();
var h = document.createElement('div');
$(h).html(p);
$(h).find('div.m-portlet').data('s', s);
$(h).find('[data-key="number"]').val(s);
$(h).find('[data-key="name"]').val("TEST");
console.log(h);
console.log($(h).html());
console.log(s);
$("div.m-content").append($(h).html());
<script id="tpl_dashboard_portlet" type="text/html">
<!--begin::Portlet-->
<div class="m-portlet">
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<h3 class="m-portlet__head-text">
<span data-key="number"></span> [<span data-key="name"></span>]
</h3>
</div>
</div>
<div class="m-portlet__head-tools">
<ul class="m-portlet_nav">
<li class="m-portlet__nav-item">
<i class="la la-close"></i>
</li>
</ul>
</div>
</div>
<!--begin::Form-->
<div class="m-portlet__body">
Found! <span data-key="number"></span> [<span data-key="name"></span>]
</div>
</div>
<!--end::Portlet-->
</script>
I'm not sure what I'm doing wrong here. I've tried using .each as well with no luck. Both leave the value of the span tags empty.
(I've removed some of the script, but the variable s does have a value on it)
You have two issues here. Firstly, every time you call $(h) you're creating a new jQuery object from the original template HTML. As such any and all previous changes you made are lost. You need to create the jQuery object from the template HTML once, then make all changes to that object.
Secondly, the span elements you select by data-key attribute do not have value properties to change, you instead need to set their text(). Try this:
var s = 'foo';
var p = $("#tpl_dashboard_portlet").html();
var $h = $('<div />');
$h.html(p);
$h.find('div.m-portlet').data('s', s);
$h.find('[data-key="number"]').text(s);
$h.find('[data-key="name"]').text("TEST");
$("div.m-content").append($h.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="tpl_dashboard_portlet" type="text/html">
<div class="m-portlet">
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<h3 class="m-portlet__head-text">
<span data-key="number"></span> [<span data-key="name"></span>]
</h3>
</div>
</div>
<div class="m-portlet__head-tools">
<ul class="m-portlet_nav">
<li class="m-portlet__nav-item">
<i class="la la-close"></i>
</li>
</ul>
</div>
</div>
<div class="m-portlet__body">
Found! <span data-key="number"></span> [<span data-key="name"></span>]
</div>
</div>
</script>
<div class="m-content"></div>
In my case only this is working:
var template = $('template').clone(true, true); // Copies all data and events
var $h = $('<div />');
$h.html(template);
$h.find('.input-name').attr('value', "your value here"); // Note: .val("your value here") is not working
$('.list').prepend($h.html());

jQuery select another element with certain class in same parent

let say i have this div
<div id='parent'>
<button id='button'></button>
<div id='child'>
<div id='grandchild' class='lookAtMe'>
Some JSON text
</div>
</div>
</div>
I wanted to give the #button an on click event that returned the text at #lookAtMe div, specific to the div whom it shares parent/grandparent (in this case, the #parent div)
I tried using:
$("#button").on("click",function(){
var ReturnedText = $(this).parent(".lookAtMe").text();
console.log(RetrunedText);
});
But the console log would retruned (empty string).
Where did i do wrong? please help. Thankyou very much.
Because there is n o parent with that class. You need find().
Actually you need to write
var ReturnedText = $(this).parent().find(".lookAtMe").text();
$("#button").on("click",function(){
var ReturnedText = $(this).parent().find(".lookAtMe").text();
console.log(ReturnedText);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='parent'>
<button id='button'></button>
<div id='child'>
<div id='grandchild' class='lookAtMe'>
Some JSON text
</div>
</div>
</div>

jQuery ID Return is undefined although HTML ID is defined

I'm trying to retrieve the ID of one element, store it as a variable and then use that ID value to interact with other elements in that section with the same ID.
<div class="mainContent">
<div class="articleContent">
<h1>header1</h1>
<p class="articlePara" id="one">para1</p>
</div>
<div class="articleFooter" id="one" onclick="readMore()">
</div>
</div>
<div class="mainContent">
<div class="articleContent">
<h1>header2</h1>
<p class="articlePara" id="two">para2</p>
</div>
<div class="articleFooter" id="two" onclick="readMore()">
</div>
</div>
And then the JS/jQuery
function readMore() {
var subID = event.target.id;
var newTarget = document.getElementById(subID).getElementsByClassName("articlePara");
alert(newTarget.id);
}
At this point I'm only trying to display the ID of the selected element but it is returning undefined and in most cases people seem to notice that jQuery is getting confused because of the differences between DOM variables and jQuery ones.
jsfiddle: https://jsfiddle.net/dr0f2nu3/
To be completely clear, I want to be able to click on one element, retrieve the ID and then select an element in the family of that clicked element using that ID value.
just remove the getElementsByClassName("articlePara"); in end of the newTarget .already you are call the element with id alert the element of the id is same with target.id
function readMore() {
var subID = event.target.id;
var newTarget = $('[id='+subID+'][class="articlePara"]')
console.log(newTarget.attr('id'));
console.log(newTarget.length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mainContent">
<div class="articleContent">
<h1>header</h1>
<p class="articlePara" id="one"></p>
</div>
<div class="articleFooter" id="one" onclick="readMore()">click
</div>
</div>
As you have read before, you should keep your id's unique, and you should avoid using onclick in html, but you could do it like this.
With querySelector you get the element and then with parentElement you can retrieve the parent of that element.
function readMore(el) {
var articleFooterId = el.id;
var articlePara = document.querySelector(".articleContent #"+articleFooterId);
var articleContent = articlePara.parentElement;
console.log('articleFooter', articleFooterId);
console.log('articlePara', articlePara);
console.log('articleContent', articleContent);
}
In your html you can return the 'this' object back to the function by doing readMore(this).
<div class="mainContent">
<div class="articleContent">
<h1>header1</h1>
<p class="articlePara" id="one">para1</p>
</div>
<div class="articleFooter" id="one" onclick="readMore(this)">footertext</div>
</div>
<div class="mainContent">
<div class="articleContent">
<h1>header2</h1>
<p class="articlePara" id="two">para2</p>
</div>
<div class="articleFooter" id="two" onclick="readMore(this)">footertext</div>
</div>
jsfiddle
if you're using Jquery:
$(function () {
$('div.articleFooter').click(function () {
var para = $(this).prev().find('p.articlePara').text();
alert('T:' + para);
});
})
$('.articleFooter').click(function() {
var b=subId; //can be any
var a="p[id="+b+"]"+"[class='articlePara']";
$(a).something;
});
You have forgotten to pass in event as parameter in your onclick= call in html.
In your javascript, you need to include event in the parenthesis as well.
window.readMore = function(event) {...}
if you write document.getElementById(subID).getElementsByClassName("articlePara"); That's saying you want to get your clicked element's CHILD elements that have class equal to articlePara . There is none. So you get undefined.
If you want to find all element with a ID one and a class articlePara, it can be done easily with jQuery:
newtarget = $("#one.articlePara");
You can insert a line: debugger; in your onclick handler function to trigger the browser's debugging tool and inspect the values of variables. Then you will know whether you are getting what you want.

JSOUP get all information starts with

<li data-id="63947814" data-author="ac ca" data-author-id="1387539" data-flags="share report vote" data-isfavorite="false" data-favorite-count="22" data-seyler-slug="" data-comment-count="0">
<div class="content"> el kadar bebelere üstelik hükümetin kıymetlisi olan bir vakıfta tecavüz edilirken üç maymunu oynayan adalet sistemimizin annenin terliğine takılması '' ... sürecek kadar akıl yok'' dedirtiyor..
<br/>ve bazı malların arzusu gerçek olursa bu sığırlar idam kararları alacaklar, kimi asacaklar ki acaba..
<br/>bilmem anlatabiliyor muyum?
</div>
<footer>
<div class="feedback"></div>
<div class="info"> <a class="entry-date permalink" href="/entry/63947814">07.11.2016 12:29</a> <a class="entry-author" href="/biri/ac-ca">ac ca</a> </div>
</footer>
<div class="comment-summary">
<div class="comment-pages"> </div>
</div>
</li>
I've got this html code.My question is i want to parse it with JSOUP like : i will get all li data-id=* informations.This website contains random data-id's so all i just wants get data starts with anything "li data-id="
Elements links = document.select("li[data-id=63947814]");
it's works only got id "63947814".
i've tried "*" : Elements links = document.select("li[data-id=*]");or
Elements links = document.select("li[data-id=\"*\"]");
but it doesnt work.Searched JSON API's and didn't get all i wanted.
so much thanks.
You should use Attribute selector, it will select all the elements with data-id attribute.
Elements links = document.select("li[data-id]");

Hide/show ul li items that contain a word. But check only in a children of the li

Markup: (lets asume that there are many childrens in the list, all with same markup)
<ul class="notifications messages inbox">
<li class=" unread">
<div class="avatar">
<img alt="" src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/276073_662982570_5177619_n.jpg">
</div>
<div class="txt">
<a class="userName" href="index.php?userid=94">Jose ignacio bustamante <span class="date"> - 2012-09-07 13:49:59</span></a>
<div>
<span>Esque esto de querer cargar una conversación por defecto... como usuario no ...</span>
</div>
</div>
<span data-id="442" class="close">X</span>
</li>
So I am trying to filter this <li> elements depending on the .username tag value ( inside of it)
/* I have used this before, its a :contains modification to handle uppercase */
$.expr[':'].icontains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
$('body').on('keyup','.filterFriends',function(){
$('.inbox li').hide().filter(':icontains("'+this.value+'")').show();
/* How to apply this icontains selector to $('.inbox li').find('.userName') ?? */
});
This has a problem, i guess it checks in the whole li, and i dont really see how to select the .userName class within the filter() function..
Can anybody show me the light?
You could try this:
$('body').on('keyup','.filterFriends',function(){
var li=$('.inbox li').hide();
$('.userName',li).filter(':icontains("'+this.value+'")').parents('li').show();
}
$('body').on('keyup','.filterFriends',function(){
$('.inbox li')
.hide()
.find('.userName')
.filter(':icontains("'+this.value+'")')
.closest('.inbox li')
.show();
});

Categories