Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if the div id in JavaScript

I have project that concerns about calendars, at first i have 1 calendar and now i want to have another one but they have different values.

<div id="cal">
....
</div>
<div id="calq">
....
</div>

my question is, how can I check if div id is "calq" in javascript?

if div.id == "calq" ?

... at first i have ...

<script type="text/javascript">
    monthYear = Date.today();
    var cal = new Calendar();
    cal.generateHTML();
    $('#cal').html(cal.getHTML());
    setMonthPrice();
    setSpecialPrice()
</script>

then i added

<script type="text/javascript">
    monthYear = Date.today();
    var calq = new Calendar();
    calq.generateHTML();
    $('#calq').html(calq.getHTML());
    setMonthQuantity();
    setSpecialQuantity();
</script>

but the setMonthQuantity() also called by cal, i just want the setMonthQuantity() only for calq

function setMonthQuantity()
{
    var weekdayBaseQuantity;
    weekdayBaseQuantity = {{ product.quantity }};

    $('td.calendar-day').append('<div class="dayquantity">' + weekdayBaseQuantity + '</div>');
    $('td.Sat .dayquantity, td.Sun .dayquantity').text( weekdayBaseQuantity );
}
like image 656
gadss Avatar asked Aug 03 '26 01:08

gadss


2 Answers

To determine the existence, in clean javascript

if(document.getElementById("calq")!='undefined')
{
    // do something, it exists 
} 

using jquery

if($("#calq").length)
{
    // do something, it exists 
}

To check the id, in clean javascript

if(this.getAttribute('id')=="calc")
{
    // do something, it exists 
}

Using jquery

if($(this).attr("id")=="calq")
{
    // do something, it exists 
}
like image 78
The Alpha Avatar answered Aug 04 '26 15:08

The Alpha


You can do check it, for example, via Jquery. I suppose that you want to make something like switch and for each div do some operation. If I'm right you can use Jquery's each function for looping against div elements and following condition for checking id's.

if($(this).attr("id")=="calq")
like image 20
Chuck Norris Avatar answered Aug 04 '26 15:08

Chuck Norris