JQuery - ID not defined. Using a dynamic ID in a function?

I'm having a bit of trouble getting a script to work within a CMS. It's a relatively simple jQuery show/hide that was working fine until I needed to use it multiple times on one page. I tried to convert it to use each DIV's individual ID, but now it returns an error in Firebug saying >id not defined. jQuery $('.articleSlide').each(function () { var current = $(this); current.attr("box_h", current.height()); $(".articleSlide").css("height", "250px"); $(".showHide").html('More'); $(".showHide a").attr("href", "javascript:void(0)"); $(".showHide a").click(function() { openSlider() }) }); function openSlider() { var open_height = $("#articleSlide_" + id).attr("box_h") + "px"; $("#articleSlide_" + id).animate({"height": open_height}, {duration: "slow" }); $(".showHide").html('More'); $(".showHide a").click(function() { closeSlider() }) } function closeSlider() { $("#articleSlide_" + id).animate({"height": "250px"}, {duration: "slow" }); $(".showHide").html('More'); $(".showHide a").click(function() { openSlider() }) } HTML

We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.

We understand markets and how to communicate in a multi-platform environment.We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.

**EDIT** OK I think I misunderstood the ID bit I included. I basically wanted it to be able to pick up the individual ID of each show/hide div so it only opened that one. This is my current script that works, but if you have more than one div on the page it opens and closes them all $(".articleSlide").each(function () { var current = $(this); current.attr("box_h", current.height()); $(".articleSlide").css("height", "250px"); $(".showHide").html('More'); $(".showHide a").attr("href", "javascript:void(0)"); $(".showHide a").click(function() { openSlider() }) }); function openSlider() { var open_height = $(".articleSlide").attr("box_h") + "px"; $(".articleSlide").animate({"height": open_height}, {duration: "slow" }); $(".showHide").html('More'); $(".showHide a").click(function() { closeSlider() }) } function closeSlider() { $(".articleSlide").animate({"height": "250px"}, {duration: "slow" }); $(".showHide").html('More'); $(".showHide a").click(function() { openSlider() }) } More

where is that id variable in the openSlider and closeSlider functions of yours defined?

以上就是JQuery - ID not defined. Using a dynamic ID in a function?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏